From 4514c0a98d323377e22181e4c49dace33d163fae Mon Sep 17 00:00:00 2001 From: Joshua Yoon Date: Tue, 8 Feb 2022 14:16:05 -0600 Subject: [PATCH 1/2] Completed Tree1, One Question --- SumRoot.java | 68 +++++++++++++++++++++++++++++++++++++++ SumRootRec.java | 38 ++++++++++++++++++++++ ValidateBST.java | 51 +++++++++++++++++++++++++++++ ValidateBSTRecursive.java | 46 ++++++++++++++++++++++++++ 4 files changed, 203 insertions(+) create mode 100644 SumRoot.java create mode 100644 SumRootRec.java create mode 100644 ValidateBST.java create mode 100644 ValidateBSTRecursive.java diff --git a/SumRoot.java b/SumRoot.java new file mode 100644 index 00000000..5e6e30ae --- /dev/null +++ b/SumRoot.java @@ -0,0 +1,68 @@ +import java.util.*; + +public class SumRoot { + + class Pair { + TreeNode node; + int value; + + public Pair(TreeNode node, int value) { + this.node = node; + this.value = value; + } + + public TreeNode getNode() { + return this.node; + } + + public int getValue() { + return this.value; + } + } + + public int sumRoot(TreeNode root) { + int result = 0; + int num = 0; + Stack s = new Stack<>(); + + while (root != null || !s.isEmpty()) { + + while (root != null) { + // N + num = num * 10 + root.val; + Pair pair = new Pair(root, num); + s.push(pair); + // L + root = root.left; + } + Pair pair = s.pop(); + root = pair.getNode(); + num = pair.getValue(); + + if (root.left == null && root.right == null) { + result += num; + } + root = root.right; + } + return result; + } + + public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } + } +} diff --git a/SumRootRec.java b/SumRootRec.java new file mode 100644 index 00000000..27223a8b --- /dev/null +++ b/SumRootRec.java @@ -0,0 +1,38 @@ +public class SumRootRec { + int result = 0; + + public int sumNumbers(TreeNode root) { + return helper(root, 0); + } + + public int helper(TreeNode root, int num) { + if (root == null) { + result += num; + return result; + } + num = num * 10 + root.val; + helper(root.left, num); + helper(root.right, num); + + return result; + } + + public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } + } +} diff --git a/ValidateBST.java b/ValidateBST.java new file mode 100644 index 00000000..b2078e2d --- /dev/null +++ b/ValidateBST.java @@ -0,0 +1,51 @@ +import java.util.*; + +class ValidateBST { + + public boolean isValidBST(TreeNode root) { + Stack s = new Stack<>(); + TreeNode prev = null; + + while (root != null || !s.isEmpty()) { + // Left + while (root != null) { + s.push(root); + root = root.left; + } + // Node + root = s.pop(); + System.out.println(root.val); + + // Compare root and prev for the num to grow (10, 25, 30, 50, 60, 70, 80) + if (prev.val >= root.val && prev != null) { + return false; + } + // Right + prev = root; + root = root.right; + } + return true; + } + + public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } + } +} +// O(n) +// O(n) +// Inorder traversal is always sorted array. \ No newline at end of file diff --git a/ValidateBSTRecursive.java b/ValidateBSTRecursive.java new file mode 100644 index 00000000..c21912c5 --- /dev/null +++ b/ValidateBSTRecursive.java @@ -0,0 +1,46 @@ + +class ValidateBSTRecursive { + + TreeNode prev = null; + + public boolean validateBSTRecursive(TreeNode root) { + return inorder(root); + } + + private boolean inorder(TreeNode root) { + if (root == null) { + return true; + } + // LNR + boolean leftResult = inorder(root.left); + if (leftResult == false) { + return false; + } + if (prev.val >= root.val && prev != null) { + return false; + } + prev = root; + System.out.println(root.val); + + return inorder(root.right); + } + + public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } + } +} \ No newline at end of file From 890700b1537edbc3bb59263eb1ad14441eb2f287 Mon Sep 17 00:00:00 2001 From: Joshua Yoon Date: Sat, 27 Jun 2026 22:22:50 -0500 Subject: [PATCH 2/2] trees-1 --- SumRoot.java | 69 ++++++--------------------------------- SumRootRec.java | 36 ++++++-------------- ValidateBST.java | 54 ++++++++---------------------- ValidateBSTRecursive.java | 49 +++++++-------------------- 4 files changed, 45 insertions(+), 163 deletions(-) diff --git a/SumRoot.java b/SumRoot.java index 5e6e30ae..6410896d 100644 --- a/SumRoot.java +++ b/SumRoot.java @@ -1,68 +1,19 @@ import java.util.*; public class SumRoot { - - class Pair { - TreeNode node; - int value; - - public Pair(TreeNode node, int value) { - this.node = node; - this.value = value; - } - - public TreeNode getNode() { - return this.node; - } - - public int getValue() { - return this.value; - } - } - + int sum; public int sumRoot(TreeNode root) { - int result = 0; - int num = 0; - Stack s = new Stack<>(); - - while (root != null || !s.isEmpty()) { - - while (root != null) { - // N - num = num * 10 + root.val; - Pair pair = new Pair(root, num); - s.push(pair); - // L - root = root.left; - } - Pair pair = s.pop(); - root = pair.getNode(); - num = pair.getValue(); - - if (root.left == null && root.right == null) { - result += num; - } - root = root.right; - } - return result; + helper(root, 0); + return sum; } + private void helper(TreeNode root, int currSum) { + if (root==null) return; - public class TreeNode { - int val; - TreeNode left; - TreeNode right; - - TreeNode() { - } - - TreeNode(int val) { - this.val = val; - } - - TreeNode(int val, TreeNode left, TreeNode right) { - this.val = val; - this.left = left; - this.right = right; + currSum = currSum * 10 + root.val; + if (root.right == null && root.left==null) { + sum += currSum; } + helper(root.left, currSum); + helper(root.right, currSum); } } diff --git a/SumRootRec.java b/SumRootRec.java index 27223a8b..3af670d6 100644 --- a/SumRootRec.java +++ b/SumRootRec.java @@ -1,38 +1,22 @@ public class SumRootRec { - int result = 0; + int sum; public int sumNumbers(TreeNode root) { return helper(root, 0); } public int helper(TreeNode root, int num) { - if (root == null) { - result += num; - return result; - } - num = num * 10 + root.val; - helper(root.left, num); - helper(root.right, num); - - return result; + helper(root, 0); + return sum; } + private void helper(TreeNode root, int currSum) { + if (root==null) return; - public class TreeNode { - int val; - TreeNode left; - TreeNode right; - - TreeNode() { - } - - TreeNode(int val) { - this.val = val; - } - - TreeNode(int val, TreeNode left, TreeNode right) { - this.val = val; - this.left = left; - this.right = right; + currSum = currSum * 10 + root.val; + if (root.right == null && root.left==null) { + sum += currSum; } + helper(root.left, currSum); + helper(root.right, currSum); } } diff --git a/ValidateBST.java b/ValidateBST.java index b2078e2d..390b1621 100644 --- a/ValidateBST.java +++ b/ValidateBST.java @@ -1,51 +1,23 @@ import java.util.*; class ValidateBST { - + boolean flag; public boolean isValidBST(TreeNode root) { - Stack s = new Stack<>(); - TreeNode prev = null; - - while (root != null || !s.isEmpty()) { - // Left - while (root != null) { - s.push(root); - root = root.left; - } - // Node - root = s.pop(); - System.out.println(root.val); - - // Compare root and prev for the num to grow (10, 25, 30, 50, 60, 70, 80) - if (prev.val >= root.val && prev != null) { - return false; - } - // Right - prev = root; - root = root.right; - } - return true; + this.flag = true; + helper(root, null, null); + return flag; } - - public class TreeNode { - int val; - TreeNode left; - TreeNode right; - - TreeNode() { + private void helper(TreeNode root, Integer min, Integer max) { + if (root==null) return; + + helper(root.left, min, root.val); + if (min != null && root.val <= min) { + flag = false; } - - TreeNode(int val) { - this.val = val; + if (max != null && root.val >= max){ + flag = false; } + helper(root.right, root.val, max); - TreeNode(int val, TreeNode left, TreeNode right) { - this.val = val; - this.left = left; - this.right = right; - } } } -// O(n) -// O(n) -// Inorder traversal is always sorted array. \ No newline at end of file diff --git a/ValidateBSTRecursive.java b/ValidateBSTRecursive.java index c21912c5..66a687c2 100644 --- a/ValidateBSTRecursive.java +++ b/ValidateBSTRecursive.java @@ -1,46 +1,21 @@ class ValidateBSTRecursive { - - TreeNode prev = null; + boolean flag; + TreeNode prev; public boolean validateBSTRecursive(TreeNode root) { - return inorder(root); + this.flag = true; + helper(root); + return flag; } - - private boolean inorder(TreeNode root) { - if (root == null) { - return true; - } - // LNR - boolean leftResult = inorder(root.left); - if (leftResult == false) { - return false; - } - if (prev.val >= root.val && prev != null) { - return false; + private void helper(TreeNode root) { + if (root==null) return; + if (!flag) return; + helper(root.left); + if (prev != null && root.val <= prev.val){ + flag = false; } prev = root; - System.out.println(root.val); - - return inorder(root.right); - } - - public class TreeNode { - int val; - TreeNode left; - TreeNode right; - - TreeNode() { - } - - TreeNode(int val) { - this.val = val; - } - - TreeNode(int val, TreeNode left, TreeNode right) { - this.val = val; - this.left = left; - this.right = right; - } + helper(root.right); } } \ No newline at end of file