題目是說,如果左右子樹都不存在又自已為0,就去掉那個子樹(設為null)

recursive後序,左子樹,右子樹,然後是根

自已同時又是別人的子樹,所以要告訢根自已是不是存在

從a開始,左右子樹都不存在,而自已是1 所以傳回true 告訢 root(c) 左子樹a 不可以刪掉(存在)

b,左右子樹都不存在,而自已是0 所以傳回false 告訢 root(c) 右子樹b 可以刪掉(不存在)

c,右子樹b可以刪除,把right = null,左子樹不可刪。雖然自已是0,可是左子樹存在所以傳回true,告訴root(e)不能刪掉自已這個子樹

d,傳回false,告誅root(e) 子樹d可以刪除

e,刪除 d子樹,傳回true

-----------------------------------------------------------

總之,左右子樹要告訢root自已可不可以被刪除,root執行刪除動作,不斷recursive

public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
} public class Solution
{
public TreeNode PruneTree(TreeNode root)
{
PostOrderTraverse(root); return root;
} bool PostOrderTraverse(TreeNode node)
{
//到leaf則此子樹沒有後續
if (node == null) return false;
//左子樹是否存在
bool isLeftExist = PostOrderTraverse(node.left);
//右子樹是否存在
bool isRightExist = PostOrderTraverse(node.right); if(isLeftExist == false)
{
node.left = null;
} if(isRightExist == false)
{
node.right = null;
} bool isThisTreeExist = false;
//根為1 或左子樹存在 或右子樹存在 則此子樹存在
if(node.val == || isLeftExist || isRightExist)
{
isThisTreeExist = true;
} return isThisTreeExist; }
}

[Leetcode] Binary Tree Pruning的更多相关文章

  1. [LeetCode] 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 Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  3. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  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 814. Binary Tree Pruning

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

  7. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  8. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  9. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. Vim文本编辑器详细用法

    1 Vi.Vim文本编辑器 1.Vi.Vim Vi是Visual interface的简称. Vim是Vi的增强版,即Vi Improved.在后面的实例中将介绍Vim的使用. 为什么学vi? 1)所 ...

  2. Django 学习笔记之模型高级用法

    目录 1 复杂的字段类型 1.1 整数类型的区别 1.2 自增类型的区别 1.3 时间类型 1.4 FilePathField 1.5 FileField 1.6 ImageField 2 关系字段 ...

  3. matplotlib 自带的几种美化样式

    1.用 matplotlib.pyplot.style.avaliable 可查看 matplotlib 自带的美化样式如下: ['bmh', 'classic', 'dark_background' ...

  4. C#进程间通讯或同步的框架引荐

    这篇文章主要介绍了一个进程间通讯同步的C#框架,代码具有相当的稳定性和可维护性,随着.NET的开源也会被注入更多活力,推荐!需要的朋友可以参考下  0.背景简介 微软在 .NET 框架中提供了多种实用 ...

  5. idea安装、配置及基本使用

    下载 下载地址:https://www.jetbrains.com/idea/download/#section=windows Ultimate为旗舰版,功能全面,插件丰富,按年收费. Commun ...

  6. 解决opencart设置SSL后评论不能翻页的问题

    为了网站的安全和seo,我们为客户的opencart网站添加了SSL加密实现https,并设置了301跳转使http跳到https,基本所有的功能都完好,就是有一点评论分页无法加载分页,去分析了链接源 ...

  7. 03-C#笔记-数据类型

    --- # 1 数据类型 --- ## 1 和C++不同的数据类型: byte: 8位,0~255 decimal:范围大于double,128位 sbyte: 8位,-128~127 uint 32 ...

  8. AtCoder Grand Contest 038题解

    好久没更了 写点东西吧= = A 01Matrix 简单构造 左上角和右下角染成1其他染成0即可 #include<bits/stdc++.h> #define ll long long ...

  9. opencv获取网络相机的图像-不用sdk

    海康相机 优点:不用sdk直接网络获取 缺点:速度有1-2秒的延迟 使用型号 1280*680分辨路 #include <iostream> #include<opencv2/ope ...

  10. Log4j之HelloWorld

    在编写项目的时候,我们一般都会用到日志记录,方便出错查找原因.首先我们需要了解什么是Log4j 1.使用maven建立工程,在pom.xml中加入如下: <dependency> < ...