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
55 changes: 55 additions & 0 deletions ConstructBTFromPreAndInOrderTraversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.HashMap;

import javax.swing.tree.TreeNode;
/**
* 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 {
/*
TC - O(n)
SC - O(n)
Solution- In preorder traversal, the first element is always the root of the current subtree. We are using
a HashMap stores the index of each value in the inorder array, allowing us to quickly find the root's position.
Starting with the first element in the preorder array, we create the root node and move to the next element
using the global index idx. Using the root's position in the inorder array, we split the tree into the left subtree
(elements before the root in inorder), and the right subtree (elements after the root in inorder).
*/
HashMap<Integer, Integer> map;
int idx;
public TreeNode buildTree(int[] preorder, int[] inorder) {
this.map = new HashMap<>();
for (int i = 0; i< inorder.length; i++) {
map.put(inorder[i], i);
}
this.idx = 0;
return helper(preorder, 0, preorder.length - 1);
}

private TreeNode helper(int[] preorder, int start, int end) {
if (start > end) {
return null;
}

int rootVal = preorder[idx];
this.idx++;
int rootIdx = map.get(rootVal);

TreeNode node = new TreeNode(rootVal);
node.left = helper(preorder, start, rootIdx - 1);
node.right = helper(preorder, rootIdx + 1, end);

return node;
}
}
49 changes: 49 additions & 0 deletions ValidBST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import javax.swing.tree.TreeNode;

/**
* 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 {
//TC - O(n)
//SC - O(h)
/*
Inthis solition we are using inorder traversal to validate the BST. The previously visited node
is saved in the prev variable and the current node's value is compared to the previously visited node.
If current value is less or equal to the prev node's value then it is not a valid BST and set the value of flag to
false. We use flag to keep track of this and when flag is false, we break out of the tree traversal.
*/
boolean flag;
TreeNode prev;
public boolean isValidBST(TreeNode root) {
flag = true;
isValid(root);
return flag;
}

private void isValid(TreeNode root) {
if (root == null) return;
if (!flag) return;

isValid(root.left);

if (prev != null && root.val <= prev.val) {
flag = false;
}

prev = root;

isValid(root.right);
}
}