From 5c29161ebe4949e70db6d13f348c97ec86340996 Mon Sep 17 00:00:00 2001 From: Praniksha123 Date: Mon, 10 Aug 2026 21:45:21 +0530 Subject: [PATCH] Create trres1.java --- trres1.java | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 trres1.java 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; + } +}