{"id":6021,"date":"2019-12-29T11:05:06","date_gmt":"2019-12-29T19:05:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6021"},"modified":"2019-12-29T16:05:24","modified_gmt":"2019-12-30T00:05:24","slug":"leetcode-1305-all-elements-in-two-binary-search-trees","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1305-all-elements-in-two-binary-search-trees\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1305. All Elements in Two Binary Search Trees"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1305. All Elements in Two Binary Search Trees - \u5237\u9898\u627e\u5de5\u4f5c EP290\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/2cbsWlAHlj4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given two binary search trees&nbsp;<code>root1<\/code>&nbsp;and&nbsp;<code>root2<\/code>.<\/p>\n\n\n\n<p>Return a list containing&nbsp;<em>all the integers<\/em>&nbsp;from&nbsp;<em>both trees<\/em>&nbsp;sorted in&nbsp;<strong>ascending<\/strong>&nbsp;order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/12\/18\/q2-e1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root1 = [2,1,4], root2 = [1,0,3]\n<strong>Output:<\/strong> [0,1,1,2,3,4]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root1 = [0,-10,10], root2 = [5,1,7,0,2]\n<strong>Output:<\/strong> [-10,0,0,1,2,5,7,10]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root1 = [], root2 = [5,1,7,0,2]\n<strong>Output:<\/strong> [0,1,2,5,7]\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root1 = [0,-10,10], root2 = []\n<strong>Output:<\/strong> [-10,0,10]\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/12\/18\/q2-e5-.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root1 = [1,null,8], root2 = [8,1]\n<strong>Output:<\/strong> [1,1,8,8]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Each tree has at most&nbsp;<code>5000<\/code>&nbsp;nodes.<\/li><li>Each node&#8217;s value is between&nbsp;<code>[-10^5, 10^5]<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Inorder traversal + Merge Sort<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(t1 + t2)<br>Space complexity: O(t1 + t2)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {    \n    function<void(TreeNode*, vector<int>&)> inorder = [&](TreeNode* root, vector<int>& t) {\n      if (!root) return;\n      inorder(root->left, t);\n      t.push_back(root->val);\n      inorder(root->right, t);\n    };\n    vector<int> t1;\n    vector<int> t2;\n    inorder(root1, t1);\n    inorder(root2, t2);\n    vector<int> m;\n    int i = 0;\n    int j = 0;\n    while (m.size() != t1.size() + t2.size()) {\n      if (j == t2.size()) m.push_back(t1[i++]);\n      else if (i == t1.size()) m.push_back(t2[j++]);\n      else m.push_back(t1[i] < t2[j] ? t1[i++] : t2[j++]);      \n    }\n    return m;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/STL<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {    \n    function<void(TreeNode*, vector<int>&)> inorder = [&](TreeNode* root, vector<int>& t) {\n      if (!root) return;\n      inorder(root->left, t);\n      t.push_back(root->val);\n      inorder(root->right, t);\n    };\n    vector<int> t1;\n    vector<int> t2;\n    inorder(root1, t1);\n    inorder(root2, t2);\n    vector<int> m;\n    std::merge(begin(t1), end(t1), begin(t2), end(t2), back_inserter(m));    \n    return m;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given two binary search trees&nbsp;root1&nbsp;and&nbsp;root2. Return a list containing&nbsp;all the integers&nbsp;from&nbsp;both trees&nbsp;sorted in&nbsp;ascending&nbsp;order. Example 1: Input: root1 = [2,1,4], root2 = [1,0,3] Output: [0,1,1,2,3,4] Example&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[74,58,321,376,32,28],"class_list":["post-6021","post","type-post","status-publish","format-standard","hentry","category-tree","tag-bst","tag-inorder","tag-mergesort","tag-on","tag-traversal","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6021"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6021\/revisions"}],"predecessor-version":[{"id":6029,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6021\/revisions\/6029"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}