diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..2851fddb --- /dev/null +++ b/Problem1.java @@ -0,0 +1,38 @@ +/** + * 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 Problem1 { + boolean flag; + TreeNode prev; + + public boolean isValidBST(TreeNode root) { + this.flag = true; + helper(root); + return flag; + } + + private void helper(TreeNode root){ + if(root == null) return; + + helper(root.left); + + if(prev != null && prev.val >= root.val){ + flag = false; + } + + prev = root; + helper(root.right); + } +} diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..eefb7839 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,43 @@ +/** + * 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 Problem2 { + public TreeNode buildTree(int[] preorder, int[] inorder) { + + // base case + if(preorder.length == 0) return null; + + int rootVal = preorder[0]; + + int rootIdx = -1; + for(int i = 0; i < inorder.length; i++){ + if(inorder[i] == rootVal){ + rootIdx = i; + break; + } + } + + int[] inLeft = Arrays.copyOfRange(inorder, 0, rootIdx); + int[] inRight = Arrays.copyOfRange(inorder, rootIdx + 1, inorder.length); + int[] preLeft = Arrays.copyOfRange(preorder, 1, inLeft.length + 1); + int[] preRight = Arrays.copyOfRange(preorder, inLeft.length + 1, preorder.length); + + TreeNode root = new TreeNode(rootVal); + root.left = buildTree(preLeft, inLeft); + root.right = buildTree(preRight, inRight); + + return root; + } +}