Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
76 changes: 76 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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;
}
}