【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 ...
随机推荐
- 自定义类实现原生SQL的GROUP_CONCAT的功能
大家都知道,原生的SQL为我们提供了分组之后查找组内数据的办法:GROUP_CONCAT方法:但是对于用Django开发的程序员来说-Django自带的ORM并没有内置这样功能的方法,而每一次遇到这样 ...
- jmeter_linux下运行
1 先把jmeter上传到linux,解压后配置环境变量(/etc/profile) 2 把在Windows上面做好的脚本上传到linux上面(linux下运行jmeter是在jmeter的bin目录 ...
- 阿里云ipv6安全组匹配所有ip的方法
IPv4和IPv6通信彼此独立.您需要为ECS实例单独配置IPv6安全组规则. 操作步骤 登录ECS控制台. 在左侧导航栏,单击网络和安全 > 安全组. 找到目标安全组,然后单击配置规则. 单击 ...
- robot framework :List Variables-List变量及其用法
[转自:https://blog.csdn.net/yezibang/article/details/52692342] 这一讲我们重点来介绍List Variables-List变量及其用法. 一. ...
- 【opencv】opencv图像识别的一些基础的基础函数的使用方法
import cv2 import numpy as np from matplotlib import pyplot as plt pic_path = "deal_with.png&qu ...
- 【EWM系列】SAP EWM创建warehouse task的函数
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP EWM创建warehouse ...
- PHPstorm Xdebugger最全详细
0 Xdebug调试的原理(选看) 图0-1 单机调试原理示意图 图0-2 多机调试原理示意图 对于PHP开发,初来咋到,开发环境的搭建和理解感觉是最烦人的一件事了.不像JAVA,打开一个Eclips ...
- python-IDE的使用(小白先看)
一.定义 IDE:集成开发环境(Integrated Development Environment) 二.常见的IDE工具: 1.VIM,经典的Linux下的文本编辑器 2.Emacs,LInux的 ...
- 将java项目转换成javaWeb项目
1.Ctrl+Shift+R快捷键:找到此项目中的.project文件,打开修改文件内容 在<natures> </natures>代码标签中,添加些内容: <natur ...
- hdu-4289.control(最小割 + 拆点)
Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...





