From 5f47113f6faf262e025a62dc90096ae180efd672 Mon Sep 17 00:00:00 2001 From: sainathek Date: Thu, 19 Mar 2026 19:58:32 -0400 Subject: [PATCH] Done Trees-1 --- problem1.java | 67 +++++++++++++++++++++++++++++++++++++++++++++ problem2.java | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 problem1.java create mode 100644 problem2.java diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..5f93bb3f --- /dev/null +++ b/problem1.java @@ -0,0 +1,67 @@ + /** + Time Complexity : O(N) + Explanation: + We perform an inorder traversal of the tree and visit each node once. + + Space Complexity : O(H) + Explanation: + Recursion stack depends on tree height. + Worst case (skewed tree) → O(N), balanced tree → O(log N). + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially tried checking only parent-child relationships, + which fails for deeper subtree violations. + Fixed it using inorder traversal because inorder traversal + of a valid BST must produce strictly increasing values. + */ + +/** + * 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 { + + boolean flag = true; + TreeNode prev; + + public boolean isValidBST(TreeNode root) { + + helper(root); + return flag; + } + + void helper(TreeNode root) { + + if (root == null) { + return; + } + + // Traverse left subtree + helper(root.left); + + // Check BST condition + if (prev != null && prev.val >= root.val) { + flag = false; + } + + // Update previous node + prev = root; + + // Traverse right subtree + helper(root.right); + } +} \ No newline at end of file diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..5c5ed797 --- /dev/null +++ b/problem2.java @@ -0,0 +1,76 @@ + + /** + Time Complexity : O(N) + Explanation: + - We traverse preorder once. + - Each node lookup in inorder is O(1) using HashMap. + So overall linear time. + + Space Complexity : O(N) + Explanation: + - HashMap stores inorder indices. + - Recursion stack can go up to N in worst case (skewed tree). + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially confused about how to split left and right subtrees. + Later understood: + - Preorder gives root first + - Inorder gives left | root | right + Used a global index for preorder and a HashMap to quickly + find root position in inorder to divide subtrees correctly. + */ +/** + * 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 idx; + HashMap map; + + public TreeNode buildTree(int[] preorder, int[] inorder) { + + idx = 0; + map = new HashMap<>(); + + // Store inorder value -> index mapping + for (int i = 0; i < inorder.length; i++) { + map.put(inorder[i], i); + } + + return helper(preorder, 0, inorder.length - 1); + } + + private TreeNode helper(int[] preorder, int start, int end) { + + // Base case + if (start > end) return null; + + // Root from preorder + int rootVal = preorder[idx]; + TreeNode root = new TreeNode(rootVal); + + int rootIdx = map.get(rootVal); + idx++; + + // Build left and right subtree + root.left = helper(preorder, start, rootIdx - 1); + root.right = helper(preorder, rootIdx + 1, end); + + return root; + } +} \ No newline at end of file