leetcode814 Binary Tree Pruning
"""
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.)
Example 1:
Input: [1,null,0,0,1]
Output: [1,null,0,null,1]
Explanation:
Only the red nodes satisfy the property "every subtree not containing a 1".
The diagram on the right represents the answer.
Example 2:
Input: [1,0,1,0,0,0,1]
Output: [1,null,1,null,1]
Example 3:
Input: [1,1,0,1,1,0,1,0]
Output: [1,1,0,1,1,null,1]
"""
"""
剪枝问题:
递归遍历,不用考虑递归左右顺序
如果结点值为0且,左右孩子为None
将该结点置为None
"""
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution:
def pruneTree(self, root):
if root == None:
return None
root.left = self.pruneTree(root.left)
root.right = self.pruneTree(root.right)
if root.val == 0 and root.left == None and root.right == None: #!!!判别式
return None
return root
leetcode814 Binary Tree Pruning的更多相关文章
- [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 ...
- Leetcode 814. Binary Tree Pruning
dfs 要点是这一句: return node.val==1 or node.left or node.right 完整代码: # Definition for a binary tree node. ...
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...
- 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 ...
随机推荐
- java 第三次课后作业
1.java字段初始化的规律 public class gouzao { public static void main(String[] args) { test te=new test(); Sy ...
- 蓝牙/zigbee/nrr24xx
目前使用的短距离无线通信技术及标准主要有Bluetooth.WIFI.ZigBee.UWB.NRF24XX系列产品等.Nordic公司生产的单片集成射频无线收发器NRF24XX系列芯片具有低功耗.支持 ...
- mysql之指定为definer的用户不存在
问题描述: java.sql.SQLException: The user specified as a definer ('tsingsoft'@'%') does not exist 解决: 1. ...
- 扒网站工具 HTTrack Website Copier
下载地址:http://www.pc6.com/softview/SoftView_30936.html 作者:匿名用户 链接:https://www.zhihu.com/question/34188 ...
- OpenTSDB 写入数据
1. 关于 Metrics, value, tag name, tag value opentsdb的每个时间序列必须有一个metric和一个或多个tag对,每个时间序列每小时的数据保存一行.open ...
- Solidity高级用法
1.内存数组: 将数组与memory结合形成内存数组,在函数调用完后就解释 uint[ ] memory values = new uint[ ](3);//初始化了一个长度为3的内存数组: #内存数 ...
- Python 基础之面向对象之装饰器@property
一.定义 装饰器@property可以把方法变成属性使用作用: 控制类内成员的获取 设置 删除获取 @property设置 @自定义名.setter删除 @自定义名.deleter 二.具体实现 1. ...
- Python之字符(2)
1.string.issupper()表示判断字符是否全部为小写字母. string1 = "abcdef" string2 = "ABCdef" string ...
- ThinkPHP6源码:从Http类的实例化看依赖注入是如何实现的
ThinkPHP 6 从原先的 App 类中分离出 Http 类,负责应用的初始化和调度等功能,而 App 类则专注于容器的管理,符合单一职责原则. 以下源码分析,我们可以从 App,Http 类的实 ...
- JavaWeb之过滤器
过滤器 什么是过滤器 1示意图: 过滤器的作用: 1.过滤器的作用好比一个保安.是servlet规范中的技术 2.用户在访问应用的资源之前或者之后,可以对请求做出一定的处理 编写过滤器步骤: 1.编写 ...