"""
Given a binary tree with the following rules:
root.val == 0
If treeNode.val == x and treeNode.left != null, then treeNode.left.val == 2 * x + 1
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
"""
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class FindElements:
def __init__(self, root):
self.array = [] # !!!保存结点出现的值,注意self. 私有成员
if root != None: # 调用递归函数
self.recover_tree(root, 0)
def recover_tree(self, root, v): # 递归调用
root.val = v
self.array.append(v)
if root.left != None:
self.recover_tree(root.left, 2 * v + 1)
if root.right != None:
self.recover_tree(root.right, 2 * v + 2)
def find(self, target: int) -> bool:
return target in self.array # 再结果数组里找

leetcode1261 Find Elements in a Contaminated Binary Tree的更多相关文章

  1. 【leetcode】1261. Find Elements in a Contaminated Binary Tree

    题目如下: Given a binary tree with the following rules: root.val == 0 If treeNode.val == x and treeNode. ...

  2. LeetCode 5264 在受污染的二叉树中查找元素 Find Elements in a Contaminated Binary Tree

    地址 https://leetcode-cn.com/contest/weekly-contest-163/problems/find-elements-in-a-contaminated-binar ...

  3. CSharp Algorithm - How to traverse binary tree by breadth (Part II)

    /* Author: Jiangong SUN */ Here I will introduce the breadth first traversal of binary tree. The pri ...

  4. 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记

    前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...

  5. [Swift]LeetCode144. 二叉树的前序遍历 | Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

  6. [Swift]LeetCode958. 二叉树的完全性检验 | Check Completeness of a Binary Tree

    Given a binary tree, determine if it is a complete binary tree. Definition of a complete binary tree ...

  7. [算法专题] Binary Tree

    1 Same Tree https://leetcode.com/problems/same-tree/ Given two binary trees, write a function to che ...

  8. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  9. 105 + 106. Construct Binary Tree from Preorder and Inorder Traversal (building trees)

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

随机推荐

  1. 最长公共子序列/子串 LCS(模板)

    首先区分子序列和子串,序列不要求连续性(连续和不连续都可以),但子串一定是连续的 1.最长公共子序列 1.最长公共子序列问题有最优子结构,这个问题可以分解称为更小的问题 2.同时,子问题的解释可以被重 ...

  2. java 连接远程Linux 服务器

    创建闭锁,确保能连接到zk服务器. // 创建闭锁final CountDownLatch countDownLatch = new CountDownLatch(1); String connect ...

  3. Windows程序设计学习笔记(1):一个简单的windows程序

    <Windows程序设计>(第五版)(美Charles Petzold著) #include<windows.h> LRESULT CALLBACK WndProc(HWND, ...

  4. Linux centos7iptables filter表案例、iptables nat表应用

    一.iptables filter表案例 vim /usr/local/sbin/iptables.sh 加入如下内容 #! /bin/bash ipt="/usr/sbin/iptable ...

  5. red hat 7、centos7的root密码破译

    一.在开机画面时按"E". 二.找到linux16开头的这段,在段尾添加空格"rd.break"然后按Ctrl+x进入系统紧急救援模式. 三.新的界面出现命令行 ...

  6. 常见Http状态码大全详解

    HTTP状态码的分类 HTTP状态码由三个十进制数字组成,第一个十进制数字定义了状态码的类型,后两个数字没有分类的作用.HTTP状态码共分为5种类型: 具体如下: 状态码 内容 详细内容 信息告知 - ...

  7. 网易云信-新增自定义消息(iOS版)

    https://www.jianshu.com/p/2bfb1c4e9f21 前言 公司业务需要,PC端,移动端都用到了第三方 网易云信 IM来实现在线客服咨询.在这当中难免遇到一些需求是网易云信没有 ...

  8. Python可视化界面编程入门

    Python可视化界面编程入门具体实现代码如所示: (1)普通可视化界面编程代码入门: import sysfrom PyQt5.QtWidgets import QWidget,QApplicati ...

  9. linux的切换目录操作

    cd 是 change directory 用法 cd [目录名] 几个特殊目录: ”.“或者”./“当前目录        ”..“或者"../"上级目录        “../ ...

  10. jqGrid一次性读取本地数据

    参考:http://blog.sina.com.cn/s/blog_54da57aa010154r7.html