leetcode1261 Find Elements in a Contaminated Binary Tree
"""
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的更多相关文章
- 【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. ...
- LeetCode 5264 在受污染的二叉树中查找元素 Find Elements in a Contaminated Binary Tree
地址 https://leetcode-cn.com/contest/weekly-contest-163/problems/find-elements-in-a-contaminated-binar ...
- 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 ...
- 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...
- [Swift]LeetCode144. 二叉树的前序遍历 | Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- [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 ...
- [算法专题] Binary Tree
1 Same Tree https://leetcode.com/problems/same-tree/ Given two binary trees, write a function to che ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- 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 ...
随机推荐
- 动态代理Cglib
jar包 <!-- https://mvnrepository.com/artifact/cglib/cglib --><dependency> <groupId> ...
- 了解 Fetch API与Fetch+Async/await
背景 提及前端与服务器端的异步通信,离不开 Ajax (Asynchronous JavaScript and XML).实际上我们常说的 Ajax 并非指某一项具体的技术,它主要是基于用脚本操作 H ...
- CSS概述(最详细!!!)
一.先综述 二.分述: 1.简介: 2.基本用法 3.引入方式: 4.盒模型 5.选择器: 6.常见文本样式及复合样式 7.改变行.块元素的属性: 8.标签显示与隐藏: ...
- NB-IOT学习
一 信号穿透力强,覆盖面广(基站少成本低).低功耗(eDRX/PSM省电技术).适合小流量时延要求不高(10s.) 二 主要芯片: 华为:Hi2110/2115,基于此的模组有:中移的M5310 移芯 ...
- MongoDB基础篇2:数据库/用户/数据集合的增删改
一.数据库操作 创建并进入数据库: 命令:use DATABASE_NAME 示例:use tms 查看所有数据库: 命令:show dbs 注意: (1)新创建的数据库在show dbs命令 ...
- Sublime设置html头部
1.Ctrl + N,新建一个文档:2.Ctrl + Shift + P,打开命令模式,再输入 sshtml 进行模糊匹配,将语法切换到html模式:3.输入 !,再按下 Tab键或者 Ctrl + ...
- autoit 《FAQ 大全》
常见问题: Q1 如何调试脚本? MsgBox(0,"测试",$var) ConsoleWrite("var=" & $var & @CRLF ...
- c-指针的理解
c-指针的理解 最近在学习MFC,其中的代码有点看的不是很深刻,究其原因还是对c语言中的指针理解的不是很好,下面详细的给大家介绍一下指针,如有不当之处,欢迎各位读者指正. 一.指针的概念 C语言里,变 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单:静态控件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- IDEA 服务器热部署详解(On Update action/On frame deactivation)
https://blog.csdn.net/w15321271041/article/details/80597962 场景:一般服务器(比如tomcat,jboss等)启动以后,我们还需要进一步修改 ...