题目如下:

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.

You need to first recover the binary tree and then implement the FindElements class:

  • FindElements(TreeNode* root) Initializes the object with a contamined binary tree, you need to recover it first.
  • bool find(int target) Return 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 <= 10^6

解题思路:题目很简单,先把树恢复,然后判断值是否存在。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class FindElements(object):
dic = {}
root = None
def __init__(self, root):
"""
:type root: TreeNode
"""
self.dic = {}
self.root = root
def recursive(node,node_val):
node.val = node_val
self.dic[node.val] = 1
if node.left != None:
recursive(node.left,node.val*2+1)
if node.right != None:
recursive(node.right,node.val*2+2)
recursive(self.root,0) def find(self, target):
"""
:type target: int
:rtype: bool
"""
return target in self.dic # Your FindElements object will be instantiated and called as such:
# obj = FindElements(root)
# param_1 = obj.find(target)

【leetcode】1261. Find Elements in a Contaminated Binary Tree的更多相关文章

  1. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  2. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  3. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】987. Vertical Order Traversal of a Binary Tree 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  5. 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  6. 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...

  7. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  8. 【leetcode】1161. Maximum Level Sum of a Binary Tree

    题目如下: Given the root of a binary tree, the level of its root is 1, the level of its children is 2, a ...

  9. 【leetcode】331. Verify Preorder Serialization of a Binary Tree

    题目如下: One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null ...

随机推荐

  1. sqlalchemy orm的cascade的参数

    #encoding: utf-8 from sqlalchemy import create_engine,Column,Integer,String,Float,func,and_,or_,Text ...

  2. lua编译器和ide

    这里有一个网址,上面记录了大部分流行的LUA开发工具,包括IDE和Editor. http://www.wowwiki.com/Lua_editors 一.Eclipse LDT 1.语法高亮,自动提 ...

  3. Python使用pycharm导入pymysql

    file->setting->project->project interperter,双击右侧出现的pip,弹出安装包,搜索pymysql->选择第一个->Instal ...

  4. BugkuCTF--域名解析(windows)

    这是这道题的题目,很简洁,flag获得的方法也告诉你了,就差把域名解析. 那么域名怎么解析呢.. 打开C:\Windows\System32\drivers\etc中的hosts文件(用记事本打开), ...

  5. leetcode题库

    leetcode题库 #题名题解通过率难度出现频率  1 两数之和     46.5%简单2 两数相加     35.5%中等3 无重复字符的最长子串     31.1%中等4 寻找两个有序数组的中位 ...

  6. 如何在云服务器上使用Docker部署easy-mock

    部署Easy-mock 安装Docker Ubuntu下安装Docker 安装Docker-compose Ubuntu下安装docker-compose 使用Docker部署 Easy-Mock D ...

  7. Abp添加新的Api(不扩展底层方法)

    定义新的实体类:FileManage;继承 FullAuditedEntity<Guid> 在XX.Application 中定义IXXservice及实现XXservice public ...

  8. ARC 100 C - Linear Approximation题解---三分法

    题目链接: https://arc100.contest.atcoder.jp/tasks/arc100_a 分析: 比赛时做这题想到一个瞎搞的方法就是在平均数上下波动一下取最小值,然后大佬yjw学长 ...

  9. 107、如何配置 Health Check ? (Swarm14)

    参考https://www.cnblogs.com/CloudMan6/p/8053323.html   容器状态是UP的,那应用就是健康的吗?    不一定   Docker 只能从容器启动进程的返 ...

  10. PL/SQL Developer13安装教程

    参考: https://blog.csdn.net/qs17809259715/article/details/88855617