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 = rig
| 14 | * } |
| 15 | */ |
| 16 | class FindElements { |
| 17 | HashSet<Integer> set = new HashSet<>(); |
| 18 | public FindElements(TreeNode root) { |
| 19 | dfs(root,0); |
| 20 | } |
| 21 | |
| 22 | public boolean find(int target) { |
| 23 | return set.contains(target); |
| 24 | |
| 25 | } |
| 26 | public void dfs(TreeNode root, int val){ |
| 27 | if(root==null) return; |
| 28 | root.val = val; |
| 29 | set.add(val); |
| 30 | dfs(root.left, 2*val+1); |
| 31 | dfs(root.right, 2*val+2); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Your FindElements object will be instantiated and called as such: |
nothing calls this directly
no outgoing calls
no test coverage detected