This is one of those problems where the clean idea matters more than the amount of code. For 1261. Find Elements in a Contaminated Binary Tree, the solution in this repository is mainly a two pointers solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are _FindElements, _find.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 888 ms, faster than 9.56% of C++ online submissions for Find Elements in a Contaminated Binary Tree.
02//Memory Usage: 17.7 MB, less than 100.00% of C++ online submissions for Find Elements in a Contaminated Binary Tree.
03
04/**
05 * Definition for a binary tree node.
06 * struct TreeNode {
07 * int val;
08 * TreeNode *left;
09 * TreeNode *right;
10 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
11 * };
12 */
13class FindElements {
14public:
15 TreeNode* myroot;
16
17 TreeNode* _FindElements(TreeNode* node, int v){
18 //preorder
19 node->val = v;
20 if(node->left) _FindElements(node->left, v*2+1);
21 if(node->right) _FindElements(node->right, v*2+2);
22 return node;
23 };
24
25 FindElements(TreeNode* root) {
26 myroot = _FindElements(root, 0);
27 }
28
29 bool _find(int target, TreeNode* node){
30 //preorder traversal
31 if(target == node->val) return true;
32 bool found = false;
33 //if find in one of its children, then that's found
34 if(node->left) found = found | _find(target, node->left);
35 if(node->right) found = found | _find(target, node->right);
36 return found;
37 }
38
39 bool find(int target) {
40 if(!myroot) return false;
41 //traverse the whole tree
42 return _find(target, myroot);
43 }
44};
45
46/**
47 * Your FindElements object will be instantiated and called as such:
48 * FindElements* obj = new FindElements(root);
49 * bool param_1 = obj->find(target);
50 */
51
52//Method 2: Using set
53//https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/discuss/431107/JavaPython-3-DFS-clean-code-w-analysis.
54//Runtime: 48 ms, faster than 44.64% of C++ online submissions for Find Elements in a Contaminated Binary Tree.
55//Memory Usage: 30.1 MB, less than 100.00% of C++ online submissions for Find Elements in a Contaminated Binary Tree.
56
57/**
58 * Definition for a binary tree node.
59 * struct TreeNode {
60 * int val;
61 * TreeNode *left;
62 * TreeNode *right;
63 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
64 * };
65 */
66class FindElements {
67public:
68 set<int> numbers;
69
70 void _FindElements(TreeNode* node, int v){
71 //preorder
72 node->val = v;
73 numbers.insert(v);
74 if(node->left) _FindElements(node->left, v*2+1);
75 if(node->right) _FindElements(node->right, v*2+2);
76 };
77
78 FindElements(TreeNode* root) {
79 _FindElements(root, 0);
80 }
81
82 bool find(int target) {
83 return (numbers.find(target) != numbers.end());
84 }
85};
86
87/**
88 * Your FindElements object will be instantiated and called as such:
89 * FindElements* obj = new FindElements(root);
90 * bool param_1 = obj->find(target);
91 */
Cost