【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 ...
随机推荐
- 职位-CFO:CFO
ylbtech-职位-CFO:CFO 首席财务官——CFO(Chief Finance Officer)是企业治理结构发展到一个新阶段的必然产物.没有首席财务官的治理结构不是现代意义上完善的治理结构. ...
- 《图解 CSS3 核心技术与案例实战》
第一章 解开 CSS3 的面纱 使用 CSS3 的好处 减少开发和维护成本:如传统实现圆角边框需要绘图.切图才能完成,而使用 css 可以直接完成 提高页面性能 渐进增强(Progressive En ...
- bp文件错误消除
代码文件报错, error: unused parameter 'data' [-Werror,-Wunused-parameter]‘ 按提示在cflags中加入: "-Wunused-p ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_02 递归_5_综合案例_文件搜索
复制上一节课的代码 这三种方式都可以获取到文件的名称 把目录的打印注释掉 如果把文件的后缀改成大写的JAVA 再获取就获取不到了 文件名或者路径 转换为小写的字符串 链式编程
- HAWQ技术总结
HAWQ技术总结: 1. 官网: http://hawq.incubator.apache.org/ 2. 特性 2.1 sql支持完善 ANSI SQL标准,OLAP扩展,标准JDBC/ODBC支持 ...
- python基础数据类型补充以及编码的进阶
一.基本数据类型的补充循环列表改变列表大小的问题#请把列表中索引为基数的元素写出l1=[1,2,3,4,5,6]for i in l1: if i%2!=0: print(i)结果:135二:基本数据 ...
- idea奇葩问题汇总
1.用idea在tomcat里运行普通的springMVC项目,用nacos做为配置中心,通过@NacosValue来读取配置中心的值,配置了autoRefreshed = true但是不起作用,读取 ...
- python+selenium元素定位之CSS学习01
参考文档:https://www.w3school.com.cn/cssref/css_selectors.asp 选择器 例子 例子描述 CSS .class .intro 选择 class=&qu ...
- Java{0}占位符替换字符串
Java{0}占位符替换字符串 public class Test { public static void main(String[] args) { System.out.println(Stri ...
- JMeter学习笔记16-如何输出HTML格式的性能测试报告
文本来学习下,如何输入HTML格式的JMeter测试报告.前面已经介绍, 如果要做性能测试,需要在GUI上设计好你的Test Plan,设置各种场景和负载值,包括多少个线程,多少个用户,循环多少次.设 ...





