11package MustDoEasyList ;
22
3+ import java .util .Stack ;
4+
35public class TrimABinarySearchTree669 {
46
57 class TreeNode {
@@ -21,7 +23,7 @@ class TreeNode {
2123 }
2224 }
2325
24- // correct one
26+ // recurssive solution
2527 public TreeNode trimBST (TreeNode root , int low , int high ) {
2628
2729 if (root == null )
@@ -41,50 +43,61 @@ public TreeNode trimBST(TreeNode root, int low, int high) {
4143 return root ;
4244 }
4345
44- // 1st approach - has bug on it
45- /*
46- * public TreeNode trimBST(TreeNode root, int low, int high) {
47- *
48- * // single node case if (root.left == null && root.right == null) { if
49- * (root.val >= low && root.val <= high) return root; else return new
50- * TreeNode(); }
51- *
52- * // if current node has no left-subtree present if (root.left == null &&
53- * root.right != null) { // ignore left- subtree and process right half only
54- * if (root.val >= low && root.val <= high) { TreeNode newNode = new
55- * TreeNode(root.val); newNode.right = trimBST(root.right, low, high);
56- * return newNode; } else { if (root.right.val >= low && root.right.val <=
57- * high) { TreeNode newNode = new TreeNode(root.right.val); newNode.left =
58- * trimBST(root.right.left, low, high); newNode.right =
59- * trimBST(root.right.right, low, high); return newNode; } else return null;
60- * } }
61- *
62- * // if current node has no right-subtree present if (root.right == null &&
63- * root.left != null) { // ignore right- subtree and process left half only
64- * if (root.val >= low && root.val <= high) { TreeNode newNode = new
65- * TreeNode(root.val); newNode.left = trimBST(root.left, low, high); return
66- * newNode; } else { if (root.left.val >= low && root.left.val <= high) {
67- * TreeNode newNode = new TreeNode(root.left.val); newNode.left =
68- * trimBST(root.left.left, low, high); newNode.right =
69- * trimBST(root.left.right, low, high); return newNode; } else return null;
70- * } }
71- *
72- * // if node has both left and right subtree
73- *
74- * if (low >= root.val) { // ignore left subtree if (root.val >= low &&
75- * root.val <= high) { TreeNode newNode = new TreeNode(root.val);
76- * newNode.right = trimBST(root.right, low, high); return newNode; } else
77- * return null;
78- *
79- * } else if (high <= root.val) { // ignore right subtree if (root.val >=
80- * low && root.val <= high) { TreeNode newNode = new TreeNode(root.val);
81- * newNode.left = trimBST(root.left, low, high); return newNode; } else
82- * return null;
83- *
84- * } else { // this will have node from both left and right subtree if
85- * (root.val >= low && root.val <= high) { TreeNode newNode = new
86- * TreeNode(root.val); newNode.left = trimBST(root.left, low, high);
87- * newNode.right = trimBST(root.right, low, high); return newNode; } else
88- * return null; } }
89- */
46+ // iterative solution
47+ public TreeNode trimBST_iterative (TreeNode root , int low , int high ) {
48+
49+ if (root == null )
50+ return root ;
51+
52+ Stack <TreeNode > stack = new Stack <>();
53+ TreeNode trimmedRoot = getTrimmedBSTRoot (root , low , high );
54+
55+ stack .push (trimmedRoot );
56+ boolean flag = false ;
57+
58+ // DFS traversal
59+ while (!stack .isEmpty ()) {
60+ TreeNode current = stack .pop ();
61+
62+ if (current != null ) {
63+
64+ if (current .left != null && current .left .val < low ) {
65+ current .left = current .left .right ;
66+ flag = true ;
67+ }
68+
69+ if (current .right != null && current .right .val > high ) {
70+ current .right = current .right .left ;
71+ flag = true ;
72+ }
73+
74+ if (flag )
75+ stack .push (current );
76+ else {
77+ if (current .left != null )
78+ stack .push (current .left );
79+ if (current .right != null )
80+ stack .push (current .right );
81+ }
82+ }
83+ flag = false ;
84+ }
85+
86+ return trimmedRoot ;
87+ }
88+
89+ private TreeNode getTrimmedBSTRoot (TreeNode root , int low , int high ) {
90+
91+ while (root != null && (root .val < low || root .val > high )) {
92+
93+ if (root .val < low )
94+ root = root .right ;
95+
96+ if (root .val > high )
97+ root = root .left ;
98+ }
99+
100+ return root ;
101+ }
102+
90103}
0 commit comments