【leetcode】1080. Insufficient Nodes in Root to Leaf Paths
题目如下:
Given the
rootof a binary tree, consider all root to leaf paths: paths from the root to any leaf. (A leaf is a node with no children.)A
nodeis insufficient if every such root to leaf path intersecting thisnodehas sum strictly less thanlimit.Delete all insufficient nodes simultaneously, and return the root of the resulting binary tree.
Example 1:
Input: root = [1,2,3,4,-99,-99,7,8,9,-99,-99,12,13,-99,14], limit = 1
Output: [1,2,3,4,null,null,7,8,9,null,14]Example 2:
Input: root = [5,4,8,11,null,17,4,7,1,null,null,5,3], limit = 22
Output: [5,4,8,11,null,17,4,7,null,null,null,5]Example 3:
Input: root = [1,2,-3,-5,null,4,null], limit = -1
Output: [1,null,-3,4]Note:
- The given tree will have between
1and5000nodes.-10^5 <= node.val <= 10^5-10^9 <= limit <= 10^9
解题思路:我的方法是两次递归,第一次递归求出每个节点所对应路径的最大值并存储在字典中,第二次递归是删除最大值小于limit的节点以及其子树。
代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
dic = {}
def calculate(self,node,number,amount):
if node.left == None and node.right == None:
self.dic[number] = node.val + amount
return node.val + amount
left_v = -float('inf')
right_v = -float('inf')
if node.left != None:
left_v = self.calculate(node.left,number*2,node.val + amount)
if node.right != None:
right_v = self.calculate(node.right,number*2+1,node.val + amount)
#node.val = max(node.val,left_v,right_v)
self.dic[number] = max(left_v,right_v)
return self.dic[number] def recursive(self,node,number,limit):
if node.left != None :
if self.dic[number*2] >= limit:
self.recursive(node.left,number*2,limit)
else:
node.left = None
if node.right != None :
if self.dic[number*2+1] >= limit:
self.recursive(node.right,number*2+1, limit)
else:
node.right = None def sufficientSubset(self, root, limit):
"""
:type root: TreeNode
:type limit: int
:rtype: TreeNode
"""
self.dic = {}
self.calculate(root,1,0)
#print self.dic
if self.dic[1] < limit:
return None
self.recursive(root,1,limit)
return root
【leetcode】1080. Insufficient Nodes in Root to Leaf Paths的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths)
Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths) 这篇是DFS专题的第一篇,所以我会 ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】1110. Delete Nodes And Return Forest 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【leetcode】1110. Delete Nodes And Return Forest
题目如下: Given the root of a binary tree, each node in the tree has a distinct value. After deleting al ...
- 【LeetCode】25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- 【LeetCode】24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【LeetCode】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 【LeetCode】25. Reverse Nodes in k-Group (2 solutions)
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- 【LeetCode】025. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
随机推荐
- 移动端调试 — iPhone +Safari
1 开启iOS设备的调试功能 打开“设置”程序,进入“Safari”->“高级”页面开启“Web检查器”: 2 mac safari浏览器设置开发者工具 safari ->偏好设置 ...
- Django学习之模板
一.常用语法 1.变量 2.Filters 3.自定义filter 4.Tags 5.csrf_token 6.注释 7.注意事项 二.母板 2.继承母板 3.块(block) 4.组件 5.静态文件 ...
- 007-Spring Boot-@Enable*注解的工作原理-EnableConfigurationProperties、ImportSelector、ImportBeanDefinitionRegistrar
一.@Enable* 启用某个特性的注解 1.EnableConfigurationProperties 回顾属性装配 application.properties中添加 tomcat.host=19 ...
- Numpy的补充(重要!!)
轴的概念 英文解释 https://www.sharpsightlabs.com/blog/numpy-axes-explained/ 汉化解释 https://www.jianshu.com/p/ ...
- eclipse 背景绿豆沙颜色
General -> Editors -> Text Editors -> Appearance color options -> Background color 色调:85 ...
- C# Thread2——线程优先级
C#中Thread的优先级不是决定每个线程被执行顺序.它决定了线程可以占用CPU的时间 Thread的优先级设置是自带的枚举类型"ThreadPriority" [ComVisib ...
- shell脚本中执行python脚本并接收其返回值的例子
1.在shell脚本执行python脚本时,需要通过python脚本的返回值来判断后面程序要执行的命令 例:有两个py程序 hello.py 复制代码代码如下: def main(): pri ...
- spring包
下载的spring包中文件及各种包众多,在项目中往往只有部分是我们必须的,如果不清楚什么时候需要什么包的话,看看下面就知道了. aspectj目录 下是在Spring框架下使用aspectj的源代码和 ...
- 手把手教你用Pytorch-Transformers——实战(二)
本文是<手把手教你用Pytorch-Transformers>的第二篇,主要讲实战 手把手教你用Pytorch-Transformers——部分源码解读及相关说明(一) 使用 PyTorc ...
- [19/05/17-星期五] HTML_body标签(内嵌标签)和框架标签
一.内嵌标签 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <!- ...





