From d59f632120c8ca270fb878827487195f23bacd1e Mon Sep 17 00:00:00 2001 From: Sarvani Baru Date: Fri, 27 Feb 2026 18:07:59 -0800 Subject: [PATCH] Done Trees-1 --- ...uctBinaryTreeFromPre-InOrderTraversal.java | 51 +++++++++++++++++++ ValidateBST.java | 48 +++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 ConstructBinaryTreeFromPre-InOrderTraversal.java create mode 100644 ValidateBST.java diff --git a/ConstructBinaryTreeFromPre-InOrderTraversal.java b/ConstructBinaryTreeFromPre-InOrderTraversal.java new file mode 100644 index 00000000..49789adf --- /dev/null +++ b/ConstructBinaryTreeFromPre-InOrderTraversal.java @@ -0,0 +1,51 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +Maintain a map to store the inorder array elements and their indices to understand the left and right elements +of root, once we identify the root from preorder array. We can have a helper method to get root element using +a pointer for each recursion and leverage that root element and map to find root's index. All the values before +that index in inorder array would be left and after the index is right. This way, we can construct the whole tree. + */ +/** + * 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 { + int index; + public TreeNode buildTree(int[] preorder, int[] inorder) { + this.index = 0; + Map map = new HashMap<>(); + for(int i = 0 ; i < inorder.length ; i++) + map.put(inorder[i], i); + return helper(preorder, 0, preorder.length - 1, map); + } + + private TreeNode helper(int[] preorder, int start, int end, Map map) { + if(start > end) + return null; + int rootVal = preorder[index]; + index++; + + int rootIndex = map.get(rootVal); + TreeNode root = new TreeNode(rootVal); + + root.left = helper(preorder, start, rootIndex - 1, map); + root.right = helper(preorder, rootIndex + 1, end, map); + return root; + } +} \ No newline at end of file diff --git a/ValidateBST.java b/ValidateBST.java new file mode 100644 index 00000000..41a67db1 --- /dev/null +++ b/ValidateBST.java @@ -0,0 +1,48 @@ +// Time Complexity : O(n) +// Space Complexity : O(h) - height of the tree +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +We use a helper method to traverse left and right children of root recursively and make sure to check +if the previous visited node's value is not greater than root value at each stack call, if so, update the +flag as false.We need to update the previous visited node with the current root to check for next stack/ +child calls. This concept/intention maps to inorder traversal where visited elements are to be in ascending +order. + */ +/** + * 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 { + TreeNode prev; + boolean flag; + + public boolean isValidBST(TreeNode root) { + this.flag = true; + validateBST(root); + return flag; + } + + private void validateBST(TreeNode root) { + if(root == null) + return; + validateBST(root.left); + if(prev != null && prev.val >= root.val) + flag = false; + prev = root; + validateBST(root.right); + } +} \ No newline at end of file