814-Binary Tree Pruning
Description:
We are given the head node root of a binary tree, where additionally every node’s value is either a 0 or a 1.
Return the same tree where every subtree (of the given tree) not containing a 1 has been removed.
(Recall that the subtree of a node X is X, plus every node that is a descendant of X.)
Note:
- The binary tree will have at most 100 nodes
- The value of each node will only be 0 or 1.
问题描述:
给定二叉树的头节点root,这个二叉树的每个节点值为0或者1.
现在要对该二叉树剪枝,对节点剪枝的条件为该节点的所有子树都为0
问题分析:
利用后序遍历将不符合条件的节点删除就可以了。
class Solution {
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) root = null;
return root;
}
}
814-Binary Tree Pruning的更多相关文章
- 814. Binary Tree Pruning(leetcode) (tree traverse)
https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode tr ...
- Leetcode 814. Binary Tree Pruning
dfs 要点是这一句: return node.val==1 or node.left or node.right 完整代码: # Definition for a binary tree node. ...
- 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...
- [Swift]LeetCode814. 二叉树剪枝 | Binary Tree Pruning
We are given the head node root of a binary tree, where additionally every node's value is either a ...
- [LeetCode] Binary Tree Pruning 二叉树修剪
We are given the head node root of a binary tree, where additionally every node's value is either a ...
- leetcode814 Binary Tree Pruning
""" We are given the head node root of a binary tree, where additionally every node's ...
- LeetCode题解之Binary Tree Pruning
1.题目描述 2.问题分析 使用递归 3.代码 TreeNode* pruneTree(TreeNode* root) { if (root == NULL) return NULL; prun(ro ...
- [Leetcode] Binary Tree Pruning
題目是說,如果左右子樹都不存在又自已為0,就去掉那個子樹(設為null) recursive後序,左子樹,右子樹,然後是根 自已同時又是別人的子樹,所以要告訢根自已是不是存在 從a開始,左右子樹都不存 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
随机推荐
- 关于HTTP以及TCP
HTTP协议 HTTP(HyperText Transport Protocol),中文译名为超文本传输协议,是一个基于TCP协议的网络协议,主要用于进行网页信息的传输. HTTP协议是在1960年由 ...
- C语言复习2_运算符
今天复习一下C语言的运算符 1.赋值运算符 单等号 = 顺序是:从右往左 2.复合运算符 #include <stdio.h> #include <stdlib.h> int ...
- python从入门到实践-4章操作列表
magicians = ['alice','david','carolina']for magician in magicians: print(magician) print(magician.ti ...
- 教你 Debug 的正确姿势——记一次 CoreMotion 的 Crash
作者:林蓝东 最近的一个手机 QQ 版本发出去后收到比较多关于 CoreMotion 的 crash 上报,案发现场如下: 但是看看这个堆栈发现它完全不按照套路出牌啊! 乍一看是挂在 CoreMoti ...
- java byte 梳理
最近写编解码的代码比较多,抽一点时间梳理下java下byte的解析.在例子代码中主要涉及的知识点就两块: 1.byte代表8个bit,其中最高位是符号位: 2.当我们用String类的getBytes ...
- C# 控制台应用程序中输出彩色字体
using System; class Example { public static void Main() { // Get a string array with the names of Co ...
- Asp.Net Core中使用Swagger,你不得不踩的坑
很久不来写blog了,换了新工作后很累,很忙.每天常态化加班到21点,偶尔还会到凌晨,加班很累,但这段时间,也确实学到了不少知识,今天这篇文章和大家分享一下:Asp.Net Core中使用Swagge ...
- 最小可用id
题目:在非负数组(乱序)中找到最小的可分配的id(从1开始编号),数据量10000000. 题目解读:在一个不重复的乱序的自然数组中找到最小的缺失的那个数,比如1,2,3,6,4,5,8,11.那么最 ...
- 对某菠菜网站的一次渗透测试 heatlevel
前言 无意间发现一个thinkphp的菠菜站,最近tp不是刚好有个漏洞吗?然后就顺手测试了一下,但过程并不太顺利,不过最后还是拿下了,所以特发此文分享下思路. 0x00 一键getshell? 简单看 ...
- 分布式服务框架介绍:最成熟的开源NIO框架Netty
尽管JDK提供了丰富的NIO类库,网上也有很多NIO学习例程,但是直接使用Java NIO类库想要开发出稳定可靠的通信框架却并非易事,原因如下: 1)NIO的类库和API繁杂,使用麻烦,你需要熟练掌握 ...