【leetcode】1261. Find Elements in a Contaminated Binary Tree
题目如下:
Given a binary tree with the following rules:
root.val == 0- If
treeNode.val == xandtreeNode.left != null, thentreeNode.left.val == 2 * x + 1- If
treeNode.val == xandtreeNode.right != null, thentreeNode.right.val == 2 * x + 2Now the binary tree is contaminated, which means all
treeNode.valhave been changed to-1.You need to first recover the binary tree and then implement the
FindElementsclass:
FindElements(TreeNode* root)Initializes the object with a contamined binary tree, you need to recover it first.bool find(int target)Return if 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 TrueExample 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 FalseExample 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 TrueConstraints:
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的更多相关文章
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】987. Vertical Order Traversal of a Binary Tree 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- # 匈牙利算法(二分图最大匹配)- hdu 过山车
匈牙利算法(二分图最大匹配)- hdu 过山车 Hdu 2063 二分图:图中的点可以分成两组U,V,所有边都是连接U,V中的顶点.等价定义是:含奇数条边的图. 匹配:一个匹配是一个边的集合,其中任意 ...
- split、paste命令
一.split分割文件 语法 split [OPTION] ... [INPUT [PREFIX]] 描述 将固定大小的INPUT输出到PREFIXaa,PREFIXab,.. ...
- Dockerfile安装jdk1.8 、部署java项目
基础指令 FROM 基于哪个镜像MAINTAINER 用来写备注信息,例如作者.日期等.COPY 复制文件进入镜像(只能用相对路径,不能用绝对路径)ADD 复制文件进入镜像(可以用绝对路径,假如是压缩 ...
- spring boot技术干货
Spring Boot2 系列教程(一)纯 Java 搭建 SSM 项目 Spring Boot2 系列教程(二)创建 Spring Boot 项目的三种方式 Spring Boot2 系列教程(三) ...
- 基于C# Socket实现多人网络聊天室
首先不多说,最终实现界面如下,可以通过点击启动服务,开启TCP服务器: 开启TCP服务器之后,可以通过点击客户端,打开一个独立的TCP客户端,打开客户端之后,输入正确的IP地址和端口号,可以进行连接服 ...
- Android官方网站!
Android官方网站,所有Android相关文档.官方工具.示例,全部都在上面!! http://www.android.com/
- Nginx的快速安装
1. 准备工作 1. CenterOS7.x.vmware虚拟机,安装过程参考 https://jingyan.baidu.com/article/eae0782787b4c01fec548535.h ...
- LVS Director端服务启动脚本
#!/bin/bash # 手动安装lpvs前端管理工具 # chkconfig: - # # lvs启动脚本:director # lvs模式类型:nat.dr.ipip # lvs代理协议:tcp ...
- 1、linux基础优化
1.添加一个用户 [root@oldboy ~]# useradd oldboy [root@oldboy ~]# id oldboy uid=500(oldboy) gid=500 (oldboy) ...
- 《python解释器源码剖析》第0章--python的架构与编译python
本系列是以陈儒先生的<python源码剖析>为学习素材,所记录的学习内容.不同的是陈儒先生的<python源码剖析>所剖析的是python2.5,本系列对应的是python3. ...


