題目是說,如果左右子樹都不存在又自已為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. hadoop hdfs 有内网、公网ip后,本地调试访问不了集群解决

    问题背景: 使用云上的虚拟环境搭建测试集群,导入一些数据,在本地idea做些debug调试,但是发现本地idea连接不上测试环境 集群内部配置hosts映射是内网映射(内网ip与主机名映射),本地只能 ...

  2. DF1协议常用命令

    PCCC:Programmable Controller Communication Commands. AB PLC常用指令 根据http://www.iatips.com/pccc_tips.ht ...

  3. Spring Cloud Sleuth + Zipkin 链路监控

    原文:https://blog.csdn.net/hubo_88/article/details/80878632 在微服务系统中,随着业务的发展,系统会变得越来越大,那么各个服务之间的调用关系也就变 ...

  4. PTA 根据后序中序遍历输出先序遍历

    本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果. 输入格式: 第一行给出正整数N(≤30),是树中结点的个数.随后两行,每行给出N个整数,分别对应后序遍历和中序遍历结果, ...

  5. vue中axios的简单使用

    我们一般在用jq的时候会使用到ajax来进行与服务器之间的交流,vue中也提供了相应的类似于ajax的方法-axios来进行与服务器之间的数据传递 现在的这篇是最简单的使用,后续会添加上来复杂的使用 ...

  6. 小程序和Vue利用swiper实现icons分页显示--动态计算

    这里发现小程序实现步骤,Vue与之类似 先上效果图: <view class="icons"> <swiper indicator-dots="true ...

  7. SpringBoot全局Jackson配置未生效

    在做一个小项目,后台服务第一次用SpringBoot构建.接口使用Json格式,在application.properties中配置如下: spring.jackson.default-propert ...

  8. 05-C#笔记-基本变量

    1. 不支持括号初始化: 2. 支持强制类型转化: 3.运算规则同C++ 参考: http://www.runoob.com/csharp/csharp-variables.html

  9. HTTP协议之chunk,单页应用这样的动态页面,怎么获取Content-Length的办法

    当客户端向服务器请求一个静态页面或者一张图片时,服务器可以很清楚的知道内容大小,然后通过Content-Length消息首部字段告诉客户端需要接收多少数据.但是如果是动态页面等时,服务器是不可能预先知 ...

  10. 【可持久化线段树】【P5826】【模板】子序列自动机

    [可持久化线段树][P5826][模板]子序列自动机 Description 给定一个序列 \(A\),有 \(q\) 次询问,每次询问一个序列 \(B\) 是不是 \(A\) 的子序列 Limita ...