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
51 changes: 51 additions & 0 deletions ConstructBinaryTreeFromPre-InOrderTraversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Time Complexity : O(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no

// Your code here along with comments explaining your approach
/*
Maintain a map to store the inorder array elements and their indices to understand the left and right elements
of root, once we identify the root from preorder array. We can have a helper method to get root element using
a pointer for each recursion and leverage that root element and map to find root's index. All the values before
that index in inorder array would be left and after the index is right. This way, we can construct the whole tree.
*/
/**
* 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 index;
public TreeNode buildTree(int[] preorder, int[] inorder) {
this.index = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0 ; i < inorder.length ; i++)
map.put(inorder[i], i);
return helper(preorder, 0, preorder.length - 1, map);
}

private TreeNode helper(int[] preorder, int start, int end, Map<Integer, Integer> map) {
if(start > end)
return null;
int rootVal = preorder[index];
index++;

int rootIndex = map.get(rootVal);
TreeNode root = new TreeNode(rootVal);

root.left = helper(preorder, start, rootIndex - 1, map);
root.right = helper(preorder, rootIndex + 1, end, map);
return root;
}
}
48 changes: 48 additions & 0 deletions ValidateBST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Time Complexity : O(n)
// Space Complexity : O(h) - height of the tree
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no

// Your code here along with comments explaining your approach
/*
We use a helper method to traverse left and right children of root recursively and make sure to check
if the previous visited node's value is not greater than root value at each stack call, if so, update the
flag as false.We need to update the previous visited node with the current root to check for next stack/
child calls. This concept/intention maps to inorder traversal where visited elements are to be in ascending
order.
*/
/**
* 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 {
TreeNode prev;
boolean flag;

public boolean isValidBST(TreeNode root) {
this.flag = true;
validateBST(root);
return flag;
}

private void validateBST(TreeNode root) {
if(root == null)
return;
validateBST(root.left);
if(prev != null && prev.val >= root.val)
flag = false;
prev = root;
validateBST(root.right);
}
}