【leetcode】1273. Delete Tree Nodes
2024-09-03 17:43:07
题目如下:
A tree rooted at node 0 is given as follows:
- The number of nodes is
nodes
;- The value of the
i
-th node isvalue[i]
;- The parent of the
i
-th node isparent[i]
.Remove every subtree whose sum of values of nodes is zero.
After doing so, return the number of nodes remaining in the tree.
Example 1:
Input: nodes = 7, parent = [-1,0,0,1,2,2,2], value = [1,-2,4,0,-2,-1,-1]
Output: 2Constraints:
1 <= nodes <= 10^4
-10^5 <= value[i] <= 10^5
parent.length == nodes
parent[0] == -1
which indicates that0
is the root.
解题思路:我的方法是递归+动态规划。对于任意一个节点i,记dp[i]为其子树的和,如果j,k....n为其子节点,那么有dp[i] = dp[j] + dp[k] + .... + dp[n] + value[i]。通过递归的方式很容易可以求出每个节点的子树和,相应的可以求出哪些节点的子树和为0。再遍历这些子树的所有节点,并标记为删除的状态,最后统计出状态为删除的节点即可。
代码如下:
class Solution(object):
def deleteTreeNodes(self, nodes, parent, value):
"""
:type nodes: int
:type parent: List[int]
:type value: List[int]
:rtype: int
"""
import sys
sys.setrecursionlimit(1000000)
dic = {}
for i in range(len(parent)):
dic[parent[i]] = dic.setdefault(parent[i],[]) + [i]
dp = [None] * nodes
def getValue(inx):
if inx not in dic:
dp[inx] = value[inx]
return value[inx]
elif dp[inx] != None:
return dp[inx]
count = 0
for child in dic[inx]:
count += getValue(child)
count += value[inx]
dp[inx] = count
return count dic_remove = {} for i in range(nodes):
if dp[i] == None:
dp[i] = getValue(i)
if dp[i] == 0: dic_remove[i] = 1 delete = [0] * nodes def markDelete(inx):
delete[inx] = 1
if inx not in dic:
return
for key in dic[inx]:
markDelete(key) for inx in dic_remove.iterkeys():
markDelete(inx)
res = sum(delete)
return nodes - res
最新文章
- [自翻]fasthttp中文文档(持续更新)
- Python3 基本数据类型
- 【krpano】高德地图导航插件(源码+介绍+预览)
- 并查集 poj2236
- 使用delphi+intraweb进行微信开发1~4代码示例
- celery简单应用
- EF6+MYSQL之初体验
- HTML5——多次定位请求
- atomic和nonatomic的区别
- HackerRank ";Minimum Average Waiting Time"; !
- sass中出现的中文问题
- angularjs学习总结(~~很详细的教程)
- Cocos2d-x v3.0正式版尝鲜体验【1】 环境搭建和新建项目
- 理解Java多态
- 对 List 、Set、Map 的理解
- 【python】字典dict
- nginx 学习 不断更新
- VMware虚拟机安装CentOS 6.9图文教程
- Windows共享设置
- safe close tcp connection