From aad7f50da92c2ea95624af65c42553a3fdf6a7cc Mon Sep 17 00:00:00 2001 From: subbu4061 Date: Fri, 30 Jan 2026 12:26:03 -0800 Subject: [PATCH 1/2] Adding all the solutions --- ValidBST.java | 46 ++++++++++++++ ...-from-inorder-and-postorder-traversal.java | 34 ++++++++++ ...e-from-preorder-and-inorder-traversal.java | 63 +++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 ValidBST.java create mode 100644 construct-binary-tree-from-inorder-and-postorder-traversal.java create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal.java diff --git a/ValidBST.java b/ValidBST.java new file mode 100644 index 00000000..09d4b772 --- /dev/null +++ b/ValidBST.java @@ -0,0 +1,46 @@ + // Solution -1 + + // TimeComplexity: O(n) where n is number of nodes + // SpaceComplexity: O(h) where h is height of the tree + // Explanation : During inorder traversal, I track the previously visited node. Because inorder traversal of a BST produces values in sorted order, any case where the previous value is greater than or equal to the current value indicates the tree is invalid, and I immediately return false. +class Solution { + TreeNode prev = null; + public boolean isValidBST(TreeNode root) { + return helper(root); + + } + private boolean helper(TreeNode root) { + //base + if(root == null) return true; + + // logic + if(!helper(root.left)) return false; + if(prev!=null){if(root.val<= prev.val)return false;} + prev = root; + return helper(root.right); + } +} + + // Solution -2 + + // TimeComplexity: O(n) where n is number of nodes + // SpaceComplexity: O(h) where h is height of the tree + // Explanation : I traverse the tree using DFS. For each node, I maintain the valid range (min, max) that its value is allowed to have based on BST properties. If the current node’s value is not within this range, I set the flag to false, meaning the tree is not a valid BST.The left subtree is recursively checked with an updated maximum bound (root.val), and the right subtree is checked with an updated minimum bound (root.val).If all nodes satisfy their constraints, the tree is a valid BST. +class Solution { + boolean flag = true; + public boolean isValidBST(TreeNode root) { + helper(root, Long.MIN_VALUE, Long.MAX_VALUE); + return flag; + + } + private void helper(TreeNode root, long min, long max) { + //base + if(root == null) return; + // logic + if(min>= root.val || max<= root.val) { + flag = false; + } + helper(root.left, min, root.val); + helper(root.right, root.val, max); + } +} \ No newline at end of file diff --git a/construct-binary-tree-from-inorder-and-postorder-traversal.java b/construct-binary-tree-from-inorder-and-postorder-traversal.java new file mode 100644 index 00000000..6460a554 --- /dev/null +++ b/construct-binary-tree-from-inorder-and-postorder-traversal.java @@ -0,0 +1,34 @@ +// TimeComplexity: O(n) + // SpaceComplexity: O(n) + // Explanation: I am building the binary tree recursively by taking the last element of the current postorder range as the root, + // because postorder visits nodes in left → right → root order. Using a HashMap of inorder values, I find the root’s position in the inorder array, + // which tells me the boundaries of the left and right subtrees. I first recursively construct the right subtree, then the left subtree, + // updating the postorder index as I go, until the entire tree is reconstructed. +class Solution { + int postIndex; + public TreeNode buildTree(int[] inorder, int[] postorder) { + postIndex = postorder.length-1; + Map inorderMap = new HashMap<>(); + for(int i=0; i < inorder.length; i++){ + inorderMap.put(inorder[i], i); + } + + return helper(postorder, inorderMap, 0 , inorder.length-1); + } + + + private TreeNode helper(int[] postorder, Map inorderMap, int inleft, int inright) { + if(inleft>inright) return null; + TreeNode root = new TreeNode(postorder[postIndex]); + postIndex--; + + + int rootidx = inorderMap.get(root.val); + root.right = helper(postorder, inorderMap,rootidx+1, inright); + root.left = helper(postorder, inorderMap,inleft, rootidx-1); + + return root; + } +} + + diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal.java b/construct-binary-tree-from-preorder-and-inorder-traversal.java new file mode 100644 index 00000000..665cb1c1 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal.java @@ -0,0 +1,63 @@ +// Solution-1 + +// TimeComplexity: O(n) +// SpaceComplexity: O(n) +// Explanation : We use preorder to pick the root nodes in order.Then we use a HashMap of inorder values to quickly find the root index. +// Then recursively build left and right subtrees using inorder boundaries. The preIndex tracks the current root in preorder. + +class Solution { + int preIndex= 0; + public TreeNode buildTree(int[] preorder, int[] inorder) { + Map inorderMap = new HashMap<>(); + for(int i=0; i < inorder.length; i++){ + inorderMap.put(inorder[i], i); + } + return helper(preorder, inorderMap, 0 , inorder.length-1); + } + + private TreeNode helper(int[] preorder, Map inorderMap, int inleft, int inright) { + if(inleft>inright) return null; + TreeNode root = new TreeNode(preorder[preIndex]); + preIndex++; + + + int rootidx = inorderMap.get(root.val); + + root.left = helper(preorder, inorderMap,inleft, rootidx-1); + root.right = helper(preorder, inorderMap,rootidx+1, inright); + return root; + } +} + +// Solution -2 + +// TimeComplexity: O(n) +// SpaceComplexity: O(n) +// Explanation : I am building the binary tree recursively by taking the first element of the current preorder range as the root. +// Using a HashMap of inorder values, I quickly find the root’s position in the inorder array, which tells me how many nodes belong to the left subtree. +// I then recursively construct the left and right subtrees by updating the preorder and inorder ranges according to the size of the left subtree, until the entire tree is built. + + +class Solution { + public TreeNode buildTree(int[] preorder, int[] inorder) { + Map inorderMap = new HashMap<>(); + for(int i=0; i < inorder.length; i++){ + inorderMap.put(inorder[i], i); + } + + + return helper(preorder, inorderMap, 0, preorder.length -1, 0 , inorder.length-1); + + + } + private TreeNode helper(int[] preorder, Map inorderMap, int preleft, int preright, int inleft, int inright) { + if(inleft>inright || preleft>preright) return null; + TreeNode root = new TreeNode(); + root.val =preorder[preleft]; + int count = inorderMap.get(root.val) - inleft; + + root.left = helper(preorder, inorderMap, preleft+1, preleft+count, inleft, inleft+count-1); + root.right = helper(preorder, inorderMap, preleft+count+1, preright, inleft+count+1, inright); + return root; + } +} From 74cd66b6bc79d783a85684627086ed59c964ddd7 Mon Sep 17 00:00:00 2001 From: subbu4061 Date: Fri, 30 Jan 2026 12:29:28 -0800 Subject: [PATCH 2/2] Removing the postorder one --- ...-from-inorder-and-postorder-traversal.java | 34 ------------------- 1 file changed, 34 deletions(-) delete mode 100644 construct-binary-tree-from-inorder-and-postorder-traversal.java diff --git a/construct-binary-tree-from-inorder-and-postorder-traversal.java b/construct-binary-tree-from-inorder-and-postorder-traversal.java deleted file mode 100644 index 6460a554..00000000 --- a/construct-binary-tree-from-inorder-and-postorder-traversal.java +++ /dev/null @@ -1,34 +0,0 @@ -// TimeComplexity: O(n) - // SpaceComplexity: O(n) - // Explanation: I am building the binary tree recursively by taking the last element of the current postorder range as the root, - // because postorder visits nodes in left → right → root order. Using a HashMap of inorder values, I find the root’s position in the inorder array, - // which tells me the boundaries of the left and right subtrees. I first recursively construct the right subtree, then the left subtree, - // updating the postorder index as I go, until the entire tree is reconstructed. -class Solution { - int postIndex; - public TreeNode buildTree(int[] inorder, int[] postorder) { - postIndex = postorder.length-1; - Map inorderMap = new HashMap<>(); - for(int i=0; i < inorder.length; i++){ - inorderMap.put(inorder[i], i); - } - - return helper(postorder, inorderMap, 0 , inorder.length-1); - } - - - private TreeNode helper(int[] postorder, Map inorderMap, int inleft, int inright) { - if(inleft>inright) return null; - TreeNode root = new TreeNode(postorder[postIndex]); - postIndex--; - - - int rootidx = inorderMap.get(root.val); - root.right = helper(postorder, inorderMap,rootidx+1, inright); - root.left = helper(postorder, inorderMap,inleft, rootidx-1); - - return root; - } -} - -