From 9579614dd71260c664000dd8b3befdc7e87e8718 Mon Sep 17 00:00:00 2001 From: Sanjoli Date: Mon, 29 Jun 2026 20:36:02 -0500 Subject: [PATCH] Completed Trees 1 --- ConstructBTFromPreAndInOrderTraversal.java | 55 ++++++++++++++++++++++ ValidBST.java | 49 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 ConstructBTFromPreAndInOrderTraversal.java create mode 100644 ValidBST.java diff --git a/ConstructBTFromPreAndInOrderTraversal.java b/ConstructBTFromPreAndInOrderTraversal.java new file mode 100644 index 00000000..4b34b541 --- /dev/null +++ b/ConstructBTFromPreAndInOrderTraversal.java @@ -0,0 +1,55 @@ +import java.util.HashMap; + +import javax.swing.tree.TreeNode; +/** + * Definition for a binary tree node. + * 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; + * } + * } + */ +class Solution { + /* + TC - O(n) + SC - O(n) + Solution- In preorder traversal, the first element is always the root of the current subtree. We are using + a HashMap stores the index of each value in the inorder array, allowing us to quickly find the root's position. + Starting with the first element in the preorder array, we create the root node and move to the next element + using the global index idx. Using the root's position in the inorder array, we split the tree into the left subtree + (elements before the root in inorder), and the right subtree (elements after the root in inorder). + */ + HashMap map; + int idx; + public TreeNode buildTree(int[] preorder, int[] inorder) { + this.map = new HashMap<>(); + for (int i = 0; i< inorder.length; i++) { + map.put(inorder[i], i); + } + this.idx = 0; + return helper(preorder, 0, preorder.length - 1); + } + + private TreeNode helper(int[] preorder, int start, int end) { + if (start > end) { + return null; + } + + int rootVal = preorder[idx]; + this.idx++; + int rootIdx = map.get(rootVal); + + TreeNode node = new TreeNode(rootVal); + node.left = helper(preorder, start, rootIdx - 1); + node.right = helper(preorder, rootIdx + 1, end); + + return node; + } +} \ No newline at end of file diff --git a/ValidBST.java b/ValidBST.java new file mode 100644 index 00000000..279cbb1f --- /dev/null +++ b/ValidBST.java @@ -0,0 +1,49 @@ +import javax.swing.tree.TreeNode; + +/** + * Definition for a binary tree node. + * 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; + * } + * } + */ +class Solution { + //TC - O(n) + //SC - O(h) + /* + Inthis solition we are using inorder traversal to validate the BST. The previously visited node + is saved in the prev variable and the current node's value is compared to the previously visited node. + If current value is less or equal to the prev node's value then it is not a valid BST and set the value of flag to + false. We use flag to keep track of this and when flag is false, we break out of the tree traversal. + */ + boolean flag; + TreeNode prev; + public boolean isValidBST(TreeNode root) { + flag = true; + isValid(root); + return flag; + } + + private void isValid(TreeNode root) { + if (root == null) return; + if (!flag) return; + + isValid(root.left); + + if (prev != null && root.val <= prev.val) { + flag = false; + } + + prev = root; + + isValid(root.right); + } +} \ No newline at end of file