1. 程式人生 > 其它 >[LeetCode] 1261. Find Elements in a Contaminated Binary Tree 在受汙染的二叉樹中查詢元素

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


Given a binary tree with the following rules:

  1. root.val == 0
  2. IftreeNode.val == xandtreeNode.left != null, thentreeNode.left.val == 2 * x + 1
  3. IftreeNode.val == xandtreeNode.right != null, thentreeNode.right.val == 2 * x + 2

Now the binary tree is contaminated, which means alltreeNode.valhave been changed to-1

.

Implement theFindElementsclass:

  • FindElements(TreeNode* root)Initializes the object with a contaminated binary tree and recovers it.
  • bool find(int target)Returnstrueif thetargetvalue 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 to20
  • The total number of nodes is between[1, 10^4]
  • Total calls offind()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 題目講解彙總(持續更新中...)


喜歡請點贊,疼愛請打賞❤️~.~


微信打賞


Venmo 打賞