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-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; + } +}