后根+分治(python)
class Solution:
def sufficientSubset(self, root: Optional[TreeNode], limit: int) -> Optional[TreeNode]:
if not root:
return None
self.limit = limit
def dfs(node,sum_):
if not (node.left or node.right):
return node.val + sum_ < self.limit
left_delete = right_delete = True
if node.left:
left_delete = dfs(node.left,sum_+node.val)
if node.right:
right_delete = dfs(node.right,sum_+node.val)
if left_delete:
node.left = None
if right_delete:
node.right = None
return left_delete and right_delete
if dfs(root,0):
return None
return root
后根+分治(java)
class Solution {
int limit = -1;
public TreeNode sufficientSubset(TreeNode root, int limit) {
this.limit = limit;
if(root == null){
return root;
}
if(dfs(root,0)){
return null;
}
return root;
}
public boolean dfs(TreeNode node,int sum){
if(node.left == null && node.right == null){
return node.val + sum < this.limit;
}
boolean remove_left = true;
boolean remove_right = true;
if(node.left != null){
remove_left = dfs(node.left,node.val + sum);
}
if(node.right != null){
remove_right = dfs(node.right,node.val + sum);
}
if(remove_left){
node.left = null;
}
if(remove_right){
node.right = null;
}
return remove_left && remove_right;
}
}
小黑生活
阴天喜茶+学习shell
JR一起刷全二环
跑完了一个人吃了麦当劳和喜茶
黑煤球请客吃串串香啦,祝贺黑驴上岸事业编
便利蜂来一波儿咖啡蛋糕之旅
研究生阶段第一次坐公交车回学校
回来和好久不见的朋友约饭,他正在参加学术会议、评国奖、入党,大忙人。。第一次喝汾酒
JR来一波儿咖啡胡同游
臭美的小黑黑
我们吃的老北京风味
下午跟随JR又去玩了飞盘
晚上吃了门框卤煮啦
最后让大家知道一下小黑对喜茶的热爱
|