"""
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 # 再结果数组里找

最新文章

  1. 双向链表、双向循环链表的JS实现
  2. php读取json时无数据(为空)的解决方法
  3. easy-ui 小白进阶史(二):操作数据,easy-ui操作
  4. MJRefresh自定义刷新动画
  5. zw版【转发·台湾nvp系列Delphi例程】HALCON SetMshape
  6. iOS - 3DTouch 3D 触摸
  7. 转:关于C++14:你需要知道的新特性
  8. mysql主从 1050错误
  9. MySQL ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO
  10. ant 配置expdp and impap
  11. python字符串实战
  12. Linux 虚存的性能问题
  13. SQL行转列与列转行(转)
  14. Mermaid js与流程图、甘特图..
  15. Nginx log日志参数详解
  16. tensorflow 文件队列
  17. Nodejs后台管理员登录实例
  18. PHP——安装wampserver丢失MSVCR110.dll
  19. C# Bulk Operations(转)
  20. VBA 选择文件

热门文章

  1. 4 CSS导航栏&下拉菜单&属性选择器&属性和值选择器
  2. Just a Hook-HDU1698 区间染色+区间查询
  3. sudo: gunicorn: command not found的问题
  4. 等级保护2.0-oracle
  5. pytest+allure(pytest-allure-adaptor基于这个插件)设计定制化报告
  6. Genymotion连接失败问题
  7. css 图形样式
  8. topthink/think-swoole 扩展包的使用 之 Task
  9. 基于IntelliJ IDEA的代码评审插件 Code Review Plugin
  10. 学习打卡8:循环语句for、while