Skip to content

[LeetCode] 1261. Find Elements in a Contaminated Binary Tree #1261

Open
@grandyang

Description

@grandyang

Given a binary tree with the following rules:

  1. root.val == 0
  2. If treeNode.val == x and treeNode.left != null, then treeNode.left.val == 2 * x + 1
  3. If treeNode.val == x and treeNode.right != null, then treeNode.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) Returns true if the target value exists in the recovered binary tree.

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
  • The height of the binary tree is less than or equal to 20
  • The total number of nodes is between [1, 10^4]
  • Total calls of 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 当作参数传入即可,参见代码如下:

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 打赏


---|---

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions