当前位置:网站首页>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;
}
}
边栏推荐
- Registration module use case writing
- MySQL 5.7.25 source code installation record
- 在.NET 6.0中配置WebHostBuilder
- E. Two Small Strings
- QT handy notes (II) edit control and float, qstring conversion
- Fuzzy PID control of motor speed
- [information system project manager] summary of essence of high-level series for the first time
- MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
- 【信息系统项目管理师】初见高项系列精华汇总
- IE7 set overflow attribute failure solution
猜你喜欢
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
面试突击68:为什么 TCP 需要 3 次握手?
Qt随手笔记(三)在vs中使用QtCharts画折线图
一种分布式深度学习编程新范式:Global Tensor
Keeping alive to realize MySQL automatic failover
图解用户登录验证流程,写得太好了!
Transform between tree and array in JS (hide the children field if the child node of the tree is empty)
Server memory failure prediction can actually do this!
Node memory overflow and V8 garbage collection mechanism
小白搞一波深拷贝 浅拷贝
随机推荐
protobuf的基本用法
JS table auto cycle scrolling, mouse move in pause
Development to testing: a six-year road to automation starting from 0
[information system project manager] summary of essence of high-level series for the first time
Interview shock 68: why does TCP need three handshakes?
QT handy notes (III) use qtcharts to draw a line chart in VS
I finished watching this video on my knees at station B
Applet record
asp. Net using redis cache (2)
Cloud native (36) | introduction and installation of harbor in kubernetes
JS judge the data types object.prototype.tostring.call and typeof
Fuzzy PID control of motor speed
2019 ICPC Asia Yinchuan regional (water problem solution)
Antd treeselect gets the value of the parent node
(2) Hand eye calibration of face scanner and manipulator (eye out of hand: nine point calibration)
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
服务发现原理分析与源码解读
JS 一行代码 获取数组最大值与最小值
Keeping alive to realize MySQL automatic failover
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛