We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Given a binary tree with the following rules:
root.val == 0
treeNode.val == x
treeNode.left != null
treeNode.left.val == 2 * x + 1
treeNode.right != null
treeNode.right.val == 2 * x + 2
Now the binary tree is contaminated, which means all treeNode.val have been changed to -1.
treeNode.val
-1
Implement the FindElements class:
FindElements
FindElements(TreeNode* root)
bool find(int target)
true
target
Example 1:
Input ["FindElements","find","find"] [[[-1,null,-1]],[1],[2]] Output [null,false,true] Explanation FindElements findElements = new FindElements([-1,null,-1]); findElements.find(1); // return False findElements.find(2); // return True
Example 2:
Input ["FindElements","find","find","find"] [[[-1,-1,-1,-1,-1]],[1],[3],[5]] Output [null,true,true,false] Explanation FindElements findElements = new FindElements([-1,-1,-1,-1,-1]); findElements.find(1); // return True findElements.find(3); // return True findElements.find(5); // return False
Example 3:
Input ["FindElements","find","find","find","find"] [[[-1,null,-1,-1,null,-1]],[2],[3],[4],[5]] Output [null,true,false,false,true] Explanation FindElements findElements = new FindElements([-1,null,-1,-1,null,-1]); findElements.find(2); // return True findElements.find(3); // return False findElements.find(4); // return False findElements.find(5); // return True
Constraints:
TreeNode.val == -1
20
[1, 10^4]
find()
0 <= target <= 106
这道题给了一棵二叉树,由于被污染了,所以每个结点值都是 -1,但是实际的命名规则是根结点值为0,且对于任意一个结点值x,若其左结点存在,则其值为 2x+1,若其右结点存在,则值为 2x+2,现在让复原给定的二叉树,同时对于给定的 target 值,判断其是否在复原的二叉树中。看了下题目中的条件,find 函数可能被调用上万次,肯定不能每次调用都遍历一遍二叉树,最快速的查找时间是常数级的,所以应该将所有的结点值都放到一个 HashSet 中,这样就能最快速的查找目标值了。这里首先要做的就是复原二叉树,在复原的过程中将结点值都存到 HashSet 中,可以用一个先序遍历,传入根结点值0。在递归函数中,若当前结点为空,直接返回,否则将传入的 val 加入 HashSet,并且赋值给当前结点值。然后判断,若左子结点存在,则对左子结点调用递归函数,并且将 2*val + 1 当作参数传入,同理,若右子结点存在,则对右子结点调用递归函数,并且将 2*val + 2 当作参数传入即可,参见代码如下:
2*val + 1
2*val + 2
class FindElements { public: FindElements(TreeNode* root) { helper(root, 0); } bool find(int target) { return st.count(target); } private: unordered_set<int> st; void helper(TreeNode* node, int val) { if (!node) return; st.insert(val); node->val = val; if (node->left) helper(node->left, 2 * val + 1); if (node->right) helper(node->right, 2 * val + 2); } };
Github 同步地址:
#1261
参考资料:
https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/
https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/discuss/431107/JavaPython-3-DFS-and-BFS-clean-codes-w-analysis.
LeetCode All in One 题目讲解汇总(持续更新中...)
喜欢请点赞,疼爱请打赏❤️~.~
微信打赏
|
Venmo 打赏
---|---
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Given a binary tree with the following rules:
root.val == 0
treeNode.val == x
andtreeNode.left != null
, thentreeNode.left.val == 2 * x + 1
treeNode.val == x
andtreeNode.right != null
, thentreeNode.right.val == 2 * x + 2
Now the binary tree is contaminated, which means all
treeNode.val
have been changed to-1
.Implement the
FindElements
class:FindElements(TreeNode* root)
Initializes the object with a contaminated binary tree and recovers it.bool find(int target)
Returnstrue
if thetarget
value exists in the recovered binary tree.Example 1:
Example 2:
Example 3:
Constraints:
TreeNode.val == -1
20
[1, 10^4]
find()
is between[1, 10^4]
0 <= target <= 106
这道题给了一棵二叉树,由于被污染了,所以每个结点值都是 -1,但是实际的命名规则是根结点值为0,且对于任意一个结点值x,若其左结点存在,则其值为 2x+1,若其右结点存在,则值为 2x+2,现在让复原给定的二叉树,同时对于给定的 target 值,判断其是否在复原的二叉树中。看了下题目中的条件,find 函数可能被调用上万次,肯定不能每次调用都遍历一遍二叉树,最快速的查找时间是常数级的,所以应该将所有的结点值都放到一个 HashSet 中,这样就能最快速的查找目标值了。这里首先要做的就是复原二叉树,在复原的过程中将结点值都存到 HashSet 中,可以用一个先序遍历,传入根结点值0。在递归函数中,若当前结点为空,直接返回,否则将传入的 val 加入 HashSet,并且赋值给当前结点值。然后判断,若左子结点存在,则对左子结点调用递归函数,并且将
2*val + 1
当作参数传入,同理,若右子结点存在,则对右子结点调用递归函数,并且将2*val + 2
当作参数传入即可,参见代码如下:Github 同步地址:
#1261
参考资料:
https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/
https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/discuss/431107/JavaPython-3-DFS-and-BFS-clean-codes-w-analysis.
LeetCode All in One 题目讲解汇总(持续更新中...)
喜欢请点赞,疼爱请打赏❤️~.~
微信打赏
|
Venmo 打赏
---|---
The text was updated successfully, but these errors were encountered: