diff --git a/trres1.java b/trres1.java new file mode 100644 index 00000000..e9d04e78 --- /dev/null +++ b/trres1.java @@ -0,0 +1,62 @@ +//problem1 +/** + * 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 { + public boolean isValidBST(TreeNode root) { + if(root==null) return false; + isValidBST(root.left); + isValidBST(root.right); + if(root.val>root.left && root.val 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, map); + } + public TreeNode helper(int[] preorder,int start,int end,HashMap map){ + if(start > end) return null; + int rootVal = preorder[idx]; + idx++; + int rootIdx = map.get(rootVal); + TreeNode root = new TreeNode(rootVal); + root.left = helper(preorder, start, rootIdx - 1, map); + root.right = helper(preorder, rootIdx + 1, end, map); + return root; + } +}