|
2 | 2 |
|
3 | 3 | import com.fishercoder.common.classes.TreeNode; |
4 | 4 |
|
5 | | -/** |
6 | | - * 776. Split BST |
7 | | - * |
8 | | - * Given a Binary Search Tree (BST) with root node root, and a target value V, |
9 | | - * split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, |
10 | | - * while the other subtree has all nodes that are greater than the target value. |
11 | | - * It's not necessarily the case that the tree contains a node with value V. |
12 | | - * Additionally, most of the structure of the original tree should remain. |
13 | | - * Formally, for any child C with parent P in the original tree, |
14 | | - * if they are both in the same subtree after the split, then node C should still have the parent P. |
15 | | - * You should output the root TreeNode of both subtrees after splitting, in any order. |
16 | | -
|
17 | | - Example 1: |
18 | | -
|
19 | | - Input: root = [4,2,6,1,3,5,7], V = 2 |
20 | | - Output: [[2,1],[4,3,6,null,null,5,7]] |
21 | | -
|
22 | | - Explanation: |
23 | | - Note that root, output[0], and output[1] are TreeNode objects, not arrays. |
24 | | -
|
25 | | - The given tree [4,2,6,1,3,5,7] is represented by the following diagram: |
26 | | -
|
27 | | - 4 |
28 | | - / \ |
29 | | - 2 6 |
30 | | - / \ / \ |
31 | | - 1 3 5 7 |
32 | | -
|
33 | | - while the diagrams for the outputs are: |
34 | | -
|
35 | | - 4 |
36 | | - / \ |
37 | | -3 6 and 2 |
38 | | - / \ / |
39 | | - 5 7 1 |
40 | | -
|
41 | | - Note: |
42 | | -
|
43 | | - The size of the BST will not exceed 50. |
44 | | - The BST is always valid and each node's value is different. |
45 | | - */ |
46 | | - |
47 | 5 | public class _776 { |
48 | | - public static class Solution1 { |
49 | | - /** credit: https://discuss.leetcode.com/topic/119481/recursive-java-solution */ |
50 | | - public TreeNode[] splitBST(TreeNode root, int V) { |
51 | | - TreeNode small = new TreeNode(0); |
52 | | - TreeNode big = new TreeNode(0); |
53 | | - split(root, V, small, big); |
54 | | - return new TreeNode[] {small.right, big.left}; |
55 | | - } |
56 | | - |
57 | | - private void split(TreeNode root, int v, TreeNode small, TreeNode big) { |
58 | | - if (root == null) { |
59 | | - return; |
60 | | - } |
61 | | - if (root.val <= v) { |
62 | | - small.right = root; |
63 | | - TreeNode right = root.right; |
64 | | - root.right = null; |
65 | | - split(right, v, root, big); |
66 | | - } else { |
67 | | - big.left = root; |
68 | | - TreeNode left = root.left; |
69 | | - root.left = null; |
70 | | - split(left, v, small, root); |
71 | | - } |
| 6 | + public static class Solution1 { |
| 7 | + /** |
| 8 | + * credit: https://discuss.leetcode.com/topic/119481/recursive-java-solution |
| 9 | + */ |
| 10 | + public TreeNode[] splitBST(TreeNode root, int V) { |
| 11 | + TreeNode small = new TreeNode(0); |
| 12 | + TreeNode big = new TreeNode(0); |
| 13 | + split(root, V, small, big); |
| 14 | + return new TreeNode[]{small.right, big.left}; |
| 15 | + } |
| 16 | + |
| 17 | + private void split(TreeNode root, int v, TreeNode small, TreeNode big) { |
| 18 | + if (root == null) { |
| 19 | + return; |
| 20 | + } |
| 21 | + if (root.val <= v) { |
| 22 | + small.right = root; |
| 23 | + TreeNode right = root.right; |
| 24 | + root.right = null; |
| 25 | + split(right, v, root, big); |
| 26 | + } else { |
| 27 | + big.left = root; |
| 28 | + TreeNode left = root.left; |
| 29 | + root.left = null; |
| 30 | + split(left, v, small, root); |
| 31 | + } |
| 32 | + } |
72 | 33 | } |
73 | | - } |
74 | 34 |
|
75 | | - public static class Solution2 { |
76 | | - /** credit: https://leetcode.com/articles/split-bst/ */ |
77 | | - public TreeNode[] splitBST(TreeNode root, int V) { |
78 | | - if (root == null) { |
79 | | - return new TreeNode[] {null, null}; |
80 | | - } else if (root.val <= V) { |
81 | | - TreeNode[] result = splitBST(root.right, V); |
82 | | - root.right = result[0]; |
83 | | - result[0] = root; |
84 | | - return result; |
85 | | - } else { |
86 | | - TreeNode[] result = splitBST(root.left, V); |
87 | | - root.left = result[1]; |
88 | | - result[1] = root; |
89 | | - return result; |
90 | | - } |
| 35 | + public static class Solution2 { |
| 36 | + /** |
| 37 | + * credit: https://leetcode.com/articles/split-bst/ |
| 38 | + */ |
| 39 | + public TreeNode[] splitBST(TreeNode root, int V) { |
| 40 | + if (root == null) { |
| 41 | + return new TreeNode[]{null, null}; |
| 42 | + } else if (root.val <= V) { |
| 43 | + TreeNode[] result = splitBST(root.right, V); |
| 44 | + root.right = result[0]; |
| 45 | + result[0] = root; |
| 46 | + return result; |
| 47 | + } else { |
| 48 | + TreeNode[] result = splitBST(root.left, V); |
| 49 | + root.left = result[1]; |
| 50 | + result[1] = root; |
| 51 | + return result; |
| 52 | + } |
| 53 | + } |
91 | 54 | } |
92 | | - } |
93 | 55 | } |
0 commit comments