|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../find-n-unique-integers-sum-up-to-zero "Find N Unique Integers Sum up to Zero") |
| 9 | + |
| 10 | +[Next >](../jump-game-iii "Jump Game III") |
| 11 | + |
| 12 | +## [1305. All Elements in Two Binary Search Trees (Medium)](https://leetcode.com/problems/all-elements-in-two-binary-search-trees "两棵二叉搜索树中的所有元素") |
| 13 | + |
| 14 | +<p>Given two binary search trees <code>root1</code> and <code>root2</code>.</p> |
| 15 | + |
| 16 | +<p>Return a list containing <em>all the integers</em> from <em>both trees</em> sorted in <strong>ascending</strong> order.</p> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | +<p><strong>Example 1:</strong></p> |
| 20 | +<img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" style="width: 457px; height: 207px;" /> |
| 21 | +<pre> |
| 22 | +<strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3] |
| 23 | +<strong>Output:</strong> [0,1,1,2,3,4] |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p><strong>Example 2:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input:</strong> root1 = [0,-10,10], root2 = [5,1,7,0,2] |
| 30 | +<strong>Output:</strong> [-10,0,0,1,2,5,7,10] |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong>Example 3:</strong></p> |
| 34 | + |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> root1 = [], root2 = [5,1,7,0,2] |
| 37 | +<strong>Output:</strong> [0,1,2,5,7] |
| 38 | +</pre> |
| 39 | + |
| 40 | +<p><strong>Example 4:</strong></p> |
| 41 | + |
| 42 | +<pre> |
| 43 | +<strong>Input:</strong> root1 = [0,-10,10], root2 = [] |
| 44 | +<strong>Output:</strong> [-10,0,10] |
| 45 | +</pre> |
| 46 | + |
| 47 | +<p><strong>Example 5:</strong></p> |
| 48 | +<img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" style="width: 352px; height: 197px;" /> |
| 49 | +<pre> |
| 50 | +<strong>Input:</strong> root1 = [1,null,8], root2 = [8,1] |
| 51 | +<strong>Output:</strong> [1,1,8,8] |
| 52 | +</pre> |
| 53 | + |
| 54 | +<p> </p> |
| 55 | +<p><strong>Constraints:</strong></p> |
| 56 | + |
| 57 | +<ul> |
| 58 | + <li>Each tree has at most <code>5000</code> nodes.</li> |
| 59 | + <li>Each node's value is between <code>[-10^5, 10^5]</code>.</li> |
| 60 | +</ul> |
| 61 | + |
| 62 | +### Related Topics |
| 63 | + [[Sort](../../tag/sort/README.md)] |
| 64 | + [[Tree](../../tag/tree/README.md)] |
| 65 | + |
| 66 | +### Hints |
| 67 | +<details> |
| 68 | +<summary>Hint 1</summary> |
| 69 | +Traverse the first tree in list1 and the second tree in list2. |
| 70 | +</details> |
| 71 | + |
| 72 | +<details> |
| 73 | +<summary>Hint 2</summary> |
| 74 | +Merge the two trees in one list and sort it. |
| 75 | +</details> |
0 commit comments