题目如下:

Given the root of a binary tree, consider all root to leaf paths: paths from the root to any leaf.  (A leaf is a node with no children.)

node is insufficient if every such root to leaf path intersecting this node has sum strictly less than limit.

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:

  1. The given tree will have between 1 and 5000 nodes.
  2. -10^5 <= node.val <= 10^5
  3. -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的更多相关文章

  1. Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths)

    Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths) 这篇是DFS专题的第一篇,所以我会 ...

  2. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  3. 【LeetCode】1110. Delete Nodes And Return Forest 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  4. 【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 ...

  5. 【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  ...

  6. 【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 ...

  7. 【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 ...

  8. 【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 ...

  9. 【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  ...

随机推荐

  1. 【洛谷P1069 细胞分裂】

    题目链接 首先,光看题就觉得它很扯淡(你哪里来这么多的钱来买试管) 根据某位已经ak过ioi的名为ych的神仙说(一看就是数学题,一看就需要因式分解,emm,我果然没有发现美的眼睛qwq) 那么我们就 ...

  2. debian 配置静态ip

    1. 查看虚拟机上本机ipcmd→ipconfig 2. 配置网卡2.1 备份原有配置文件配置文件cp /etc/network/interfaces /etc/network/interfacesb ...

  3. json模块:字符串与字典之间的转换--loads,dumps,load,dump

    一.json转化成字典: product.json文件:将该文件内容转换成python中字典,方法如下: 方法一:使用.loads(),先读后转换 import  json   #导入json, 注: ...

  4. charles之抓取浏览器https请求

    用charles抓取浏览器https的包时,请求显示为unknown,且请求和响应数据乱码,本篇介绍如何抓取正常响应的https请求 目录 1.安装charles 2.安装证书.添加域名 3.抓包 1 ...

  5. PHP 实现并发-进程控制 PCNTL

    参考 基于PCNTL的PHP并发编程 PCNTL 是 PHP 中的一组进程控制函数,可以用来 fork(创建)进程,传输控制信号等. 在PHP中,进程控制支持默认关闭.编译时通过 --enable-p ...

  6. 小程序-web-view嵌入H5页面遇到的bug

    遇到的问题1:ios页面中,内容过多时有下滑真是功能,但是下滑的时候回看到底部的微信自带的灰色背景及H5的域名(ios的webview中上/下拉露出黑灰色背景问题) 解决办法:给body添加样式--- ...

  7. vue猜数字游戏

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 今天起,重新开头学习Java - 一、安装环境

    先拜领路人 https://blog.csdn.net/u011541946/article/category/6951961/3? 一.安装JDK 1. 下载 www.java.com JDK是Ja ...

  9. 知乎使用selenium反爬虫的解决方案

    from selenium.webdriver import Chrome from selenium.webdriver import ChromeOptions option = ChromeOp ...

  10. body传参?parameter传参?Request Payload?Query String Parameter?

    今天,是有委屈的一天:今天,是有小情绪的一天.所以,我们要对今天进行小结,跟它做一个了断! 今天,后端来一个接口,告诉我"要用post请求,parameter形式传参".over. ...