題目是說,如果左右子樹都不存在又自已為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. svn忽略target文件

    背景:最近项目转移到svn上 发现:项目从svn拉取下来到eclipse中,发现有大量的文件改动,一看都是一些.project之类的配置文件或者是target文件夹,或者下面的文件 这些东西肯定是不需 ...

  2. mysql数据库之运行时其他报错

    This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法 这是我们开启了bin-log, ...

  3. Iconfont技术

    什么是 IconFont 顾名思义,IconFont 就是字体图标.严格地说,就是一种字体,但是,它们不包含字母或数字,而是包含符号和字形.您可以使用 CSS 设置样式,就像设置常规文本一样,这使得 ...

  4. 数据库备份 DBS(Database Backup),知识点

    资料 网址 什么是DBS https://help.aliyun.com/document_detail/59133.html?spm=5176.13685554.103.6.3fa463f9CDwW ...

  5. rn相关文档

    RN相关文档: rn文档:https://reactnative.cn/ mbox文档:https://cn.mobx.js.org/ es6文档:http://es6.ruanyifeng.com/ ...

  6. 【MyEclipse异常】更换工作空间无法启动的异常

  7. monkey--常用参数

    前戏 参数分类:常规类参数,事件类参数,约束类参数,调试类参数 常规类参数:常规类参数包括帮助参数和日志信息参数,帮助参数用于输出monkey命令使用指导,日志信息参数将日志分为三个等级,级别越高,日 ...

  8. 学好Python后可从事岗位+学习Python的难度

    一.学好Python好就业: 1.Linux运维.Linux运维是必须而且一定要掌握Python语言,Python可以满足Linux运维工程师的工作需求提升效率,总而提升自己的能力.用Python实现 ...

  9. Django ORM性能优化 和 图片验证码

    一,ORM性能相关 1. 关联外键, 只拿一次数据 all_users = models.User.objects.all().values('name', 'age', 'role__name') ...

  10. SQL数据同步到ELK(一)- 日常开篇

    需求 在我们的实际业务中,业务数据大部分是通过传统DB做持久化,但有时会使用Solr/Elastic Search等做搜索.缓存等其他服务,那么如何将数据同步到这些异构的存储系统中呢? 这就是我最近在 ...