列表

详情


1261. 在受污染的二叉树中查找元素

给出一个满足下述规则的二叉树:

  1. root.val == 0
  2. 如果 treeNode.val == x 且 treeNode.left != null,那么 treeNode.left.val == 2 * x + 1
  3. 如果 treeNode.val == xtreeNode.right != null,那么 treeNode.right.val == 2 * x + 2

现在这个二叉树受到「污染」,所有的 treeNode.val 都变成了 -1

请你先还原二叉树,然后实现 FindElements 类:

 

示例 1:

输入:
["FindElements","find","find"]
[[[-1,null,-1]],[1],[2]]
输出:
[null,false,true]
解释:
FindElements findElements = new FindElements([-1,null,-1]); 
findElements.find(1); // return False 
findElements.find(2); // return True 

示例 2:

输入:
["FindElements","find","find","find"]
[[[-1,-1,-1,-1,-1]],[1],[3],[5]]
输出:
[null,true,true,false]
解释:
FindElements findElements = new FindElements([-1,-1,-1,-1,-1]);
findElements.find(1); // return True
findElements.find(3); // return True
findElements.find(5); // return False

示例 3:

输入:
["FindElements","find","find","find","find"]
[[[-1,null,-1,-1,null,-1]],[2],[3],[4],[5]]
输出:
[null,true,false,false,true]
解释:
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

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class FindElements { public: FindElements(TreeNode* root) { } bool find(int target) { } }; /** * Your FindElements object will be instantiated and called as such: * FindElements* obj = new FindElements(root); * bool param_1 = obj->find(target); */

php 解法, 执行用时: 102 ms, 内存消耗: 24.5 MB, 提交时间: 2024-03-12 14:19:27

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class FindElements {
    /**
     * @param TreeNode $root
     */
    function __construct($root) {
        $this->ans = [];
        $this->root = $root;
        if ( $root ) {
            $this->root->val = 0;
            $this->ans[] = 0;
            $this->dfs($this->root);
        }
    }
    
    function dfs($node) {
        if ( $node == null )
            return;
        
        if ( $node->left ) {
            $node->left->val = 2 * $node->val + 1;
            $this->ans[] = $node->left->val;
            $this->dfs($node->left);
        }
        
        if ( $node->right ) {
            $node->right->val = 2 * $node->val + 2;
            $this->ans[] = $node->right->val;
            $this->dfs($node->right);
        }
    }

    /**
     * @param Integer $target
     * @return Boolean
     */
    function find($target) {
        return in_array($target, $this->ans);
    }
}

/**
 * Your FindElements object will be instantiated and called as such:
 * $obj = FindElements($root);
 * $ret_1 = $obj->find($target);
 */

golang 解法, 执行用时: 24 ms, 内存消耗: 7.4 MB, 提交时间: 2022-11-17 11:17:20

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
type FindElements struct {
	aset map[int]struct{}
}

func Constructor(root *TreeNode) FindElements {
	aset := make(map[int]struct{})
	var dfs func(node *TreeNode, val int)
	dfs = func(node *TreeNode, val int) {
		if node != nil {
			node.Val = val
			aset[val] = struct{}{}
			dfs(node.Left, val*2+1)
			dfs(node.Right, val*2+2)
		}
	}
	dfs(root, 0)
	return FindElements{
		aset: aset,
	}
}

func (this *FindElements) Find(target int) bool {
	_, ok := this.aset[target]
	return ok
}


/**
 * Your FindElements object will be instantiated and called as such:
 * obj := Constructor(root);
 * param_1 := obj.Find(target);
 */

python3 解法, 执行用时: 676 ms, 内存消耗: 18.9 MB, 提交时间: 2022-11-17 11:15:01

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class FindElements:

    def __init__(self, root: Optional[TreeNode]):
        self.ans = []
        self.root = root
        if root:
            self.root.val = 0
            self.ans.append(0)
            self.dfs(self.root)
        
        
    def dfs(self, node: TreeNode):
        if not node:
            return
        
        if node.left:
            node.left.val = 2 * node.val + 1
            self.ans.append(node.left.val)
            self.dfs(node.left)
        
        if node.right:
            node.right.val = 2 * node.val + 2
            self.ans.append(node.right.val)
            self.dfs(node.right)
        


    def find(self, target: int) -> bool:
        return target in self.ans
        


# Your FindElements object will be instantiated and called as such:
# obj = FindElements(root)
# param_1 = obj.find(target)

python3 解法, 执行用时: 792 ms, 内存消耗: 18.9 MB, 提交时间: 2022-11-17 11:03:09

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class FindElements:

    def __init__(self, root: Optional[TreeNode]):
        def fill(root, key):
            if not root:
                return
            else:
                root.val = key
                fill(root.left, 2*key + 1)
                fill(root.right, 2*key + 2)
        fill(root, 0)
        
        self.ans = []
        
        def inorder(root):
            if not root:
                return 
            else:
                inorder(root.left)
                self.ans.append(root.val)
                inorder(root.right)
        inorder(root)
        


    def find(self, target: int) -> bool:
        return target in self.ans
        


# Your FindElements object will be instantiated and called as such:
# obj = FindElements(root)
# param_1 = obj.find(target)

上一题