当前位置:网站首页>Li Kou - binary tree pruning
Li Kou - binary tree pruning
2022-07-26 09:55:00 【Xiao Tang Xuejie】
Two fork tree pruning
Give you the root node of the binary tree root , In addition, the value of each node of the tree is either 0 , Or 1 .
Return to remove all excluded 1 The original binary tree of the subtree .
node node The subtree of is node Plus all itself node The offspring of .
Example 1:
Input :root = [1,null,0,0,1]
Output :[1,null,0,null,1]
explain :
Only the red nodes meet the conditions “ All do not contain 1 The subtree of ”. On the right is the answer .
Example 2:
Input :root = [1,0,1,0,0,0,1]
Output :[1,null,1,null,1]
Example 3:
Input :root = [1,1,0,1,1,0,1,0]
Output :[1,1,0,1,1,null,1]
source : Power button (LeetCode)
link :https://leetcode.cn/problems/binary-tree-pruning
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
// Subsequent traversal plus recursion
public TreeNode pruneTree(TreeNode root) {
if(root == null) {
return null;
}
root.left = pruneTree(root.left);
root.right = pruneTree(root.right);
if(root.left == null && root.right == null && root.val == 0) {
return null;
}
return root;
}
}边栏推荐
- 服务发现原理分析与源码解读
- IIS website configuration
- POJ 1012 Joseph
- Node 内存溢出及V8垃圾回收机制
- Write a script that can run in Bash / shell and PowerShell
- WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
- 服务器内存故障预测居然可以这样做!
- Set view dynamic picture
- The problem of accessing certsrv after configuring ADCs
- Login module use case writing
猜你喜欢

R language ggplot2 visualization: align the legend title to the middle of the legend box in ggplot2 (default left alignment, align legend title to middle of legend)

解决ProxyError: Conda cannot proceed due to an error in your proxy configuration.

asp. Net using redis cache

【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛

高斯消元求解矩阵的逆(gauss)

Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning

2022 zhongkepan cloud - server internal information acquisition and analysis flag

Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market

Leetcode 504. 七进制数

Gauss elimination
随机推荐
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
莫队学习总结(二)
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
云原生(三十六) | Kubernetes篇之Harbor入门和安装
小白搞一波深拷贝 浅拷贝
Why does new public chain Aptos meet market expectations?
Usage of the formatter attribute of El table
Double authentication of server and client
论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)
SSG framework Gatsby accesses the database and displays it on the page
分布式网络通信框架:本地服务怎么发布成RPC服务
Spolicy request case
Gauss elimination
The use of MySQL in nodejs
WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
Solve NPM -v sudden failure and no response
JS one line code to obtain the maximum and minimum values of the array
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
Production of a-modal drag function in antui