当前位置:网站首页>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;
}
}
边栏推荐
- Nodejs service background execution (forever)
- AR model in MATLAB for short-term traffic flow prediction
- 阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践
- Principle analysis and source code interpretation of service discovery
- Docker configuring MySQL Cluster
- MFC handy notes
- 服务器内存故障预测居然可以这样做!
- R language ggpubr package ggsummarystats function visualizes the grouping box diagram (custom grouping color) and adds the statistical values corresponding to the grouping under the x-axis label (samp
- The problem of accessing certsrv after configuring ADCs
- asp. Net using redis cache
猜你喜欢
asp. Net using redis cache
一种分布式深度学习编程新范式:Global Tensor
2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
新增市场竞争激烈,中国移动被迫推出限制性超低价5G套餐
Node memory overflow and V8 garbage collection mechanism
Mysql5.7.25 master-slave replication (one-way)
2019 ICPC Asia Yinchuan regional (water problem solution)
Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)
阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践
Server and client dual authentication (2)
随机推荐
Redis sentinel mode setup under Windows
Use of tabbarcontroller
Cloud native (36) | introduction and installation of harbor in kubernetes
服务器内存故障预测居然可以这样做!
The problem of accessing certsrv after configuring ADCs
Development to testing: a six-year road to automation starting from 0
Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
[information system project manager] summary of essence of high-level series for the first time
分布式网络通信框架:本地服务怎么发布成RPC服务
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)
Wechat applet learning notes 2
Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)
JS 连等赋值操作
时间序列异常检测
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
学习笔记之常用数组api 改变原数组和不改变原数组的有哪些?
小白搞一波深拷贝 浅拷贝
Solve NPM -v sudden failure and no response
IIS error prompt after installing Serv-U: hresult:0x80070020
El table implements adding / deleting rows, and a parameter changes accordingly