当前位置:网站首页>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;
}
}边栏推荐
- (2) Hand eye calibration of face scanner and manipulator (eye out of hand: nine point calibration)
- 新增市场竞争激烈,中国移动被迫推出限制性超低价5G套餐
- Gauss elimination solves the inverse of matrix (Gauss)
- PHP one-time request lifecycle
- 高斯消元
- m进制数str转n进制数
- Double authentication of server and client
- 面试突击68:为什么 TCP 需要 3 次握手?
- Gauss elimination
- Write a script that can run in Bash / shell and PowerShell
猜你喜欢

2021年山东省中职组“网络空间安全”B模块windows渗透(解析)

Fiddler packet capturing tool for mobile packet capturing

Installation and use of cocoapods

2022 zhongkepan cloud - server internal information acquisition and analysis flag

Principle analysis and source code interpretation of service discovery

Principle analysis and source code interpretation of service discovery

2019 ICPC Asia Yinchuan regional (water problem solution)

Server and client dual authentication (2)

Logical architecture of MySQL

论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)
随机推荐
网络流学习笔记
Fiddler packet capturing tool for mobile packet capturing
2022 zhongkepan cloud - server internal information acquisition and analysis flag
SSG框架Gatsby访问数据库,并显示到页面上
Fiddler download and installation
Application of Gauss elimination
(1) Hand eye calibration of face scanner and manipulator (eye on hand)
莫队学习笔记(一)
Fuzzy PID control of motor speed
[fluorescent character effect]
Mysql5.7.25 master-slave replication (one-way)
Qt随手笔记(三)在vs中使用QtCharts画折线图
Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
Sublime install plug-ins
Modern medicine in the era of "Internet +"
Vectortilelayer replacement style
Encapsulation of tabbarcontroller
开发转测试:从0开始的6年自动化之路...
Tableviewcell highly adaptive
2019 ICPC Asia Yinchuan Regional(水题题解)