[LeetCode] 1261. Find Elements in a Contaminated Binary Tree 在受污染的二叉樹中查找元素



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 同步地址:

https://github.com/grandyang/leetcode/issues/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 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM