|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import com.fishercoder.common.classes.TreeNode; |
| 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 | +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 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 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 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments