"""
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的更多相关文章

  1. [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 ...

  2. [LeetCode] Binary Tree Pruning 二叉树修剪

    We are given the head node root of a binary tree, where additionally every node's value is either a ...

  3. Leetcode 814. Binary Tree Pruning

    dfs 要点是这一句: return node.val==1 or node.left or node.right 完整代码: # Definition for a binary tree node. ...

  4. 814. Binary Tree Pruning(leetcode) (tree traverse)

    https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode tr ...

  5. 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...

  6. LeetCode题解之Binary Tree Pruning

    1.题目描述 2.问题分析 使用递归 3.代码 TreeNode* pruneTree(TreeNode* root) { if (root == NULL) return NULL; prun(ro ...

  7. [Leetcode] Binary Tree Pruning

    題目是說,如果左右子樹都不存在又自已為0,就去掉那個子樹(設為null) recursive後序,左子樹,右子樹,然後是根 自已同時又是別人的子樹,所以要告訢根自已是不是存在 從a開始,左右子樹都不存 ...

  8. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

随机推荐

  1. N3K license安装

    1.获取设备SN和PAK SN获取: Switch#show license host-id 注意:IOS设备中为:show license udi PAK获取: PAK是单独购买license后,c ...

  2. day 12 zuoye

    复习 # 函数 -- 2天 # 函数的定义和调用 # def 函数名(形参): #函数体 #return 返回值 #调用 函数名(实参) # 站在形参的角度上 : 位置参数,*args,默认参数(陷阱 ...

  3. 【原】mac电脑常用快捷建

    wiki:http://www.wjjsoft.com/mybase_cn.html 1.终端 有时一个终端并不够,如何快速打开多个呢,在上部打开之伤的基础下,直接按快捷键command+N即可 一个 ...

  4. navicat for mysql 中文破解版(无需激活码)

    1.下载破解版Navicat for MySQ中文破解版 链接: https://pan.baidu.com/s/19BlJRx7tLnDPIM4U6DNWvA 提取码: d9ci 2.解压下载后的文 ...

  5. 弱密码检测JR!

    1.JR(Joth the Ripper)简介·一款密码分析工具,支持字典式的暴力破解·通过对 shadow 文件的口令分析,可以检测密码·官方网站:http://www.openwall.com/j ...

  6. python 之并发编程更新版进程池与进程池比较与回调函数

    一.更新版进程池与进程池比较 from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor import os, tim ...

  7. sqlmap命令手册

    http://127.0.0.1/sqli-labs-master/Less-1/?id=1 当给sqlmap上面这么一个url的时候,它会自动: 1.判断可注入的参数 2.判断可以用那种SQL注入技 ...

  8. 在javaweb中对于session的使用

    1.初次调用session时: String username="student"; HttpSession session=request.getSession(true);// ...

  9. nginx 的磁盘IO优化

    磁盘IO优化的几个方面 优化读取 Sendfile 零拷贝.内存盘.SSD盘 减少写入 AIO 增大error_log级别的日志 关闭access_log  压缩access_log 是否启用prox ...

  10. vue-element-admin 引入高德地图并做海量点标记

    第一步: 首先在index.html入口文件中添加引入高德地图的js,并填写自己在官网申请的key.如果没有申请不填写也是可以的. plugin:项目中如果有需要引入插件则使用没有直接去掉就行. &l ...