MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / FindElements

Class FindElements

FindElementsInAContaminatedBinaryTree.java:16–33  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

14 * }
15 */
16class 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:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected