題目是說,如果左右子樹都不存在又自已為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. springboot日常问题处理手记

    springboot启动问题 1.@Autowired报错Could not autowire. No beans of xxx 解决:只需在DAO接口加上@Component 或者 @Reposit ...

  2. Linux(Centos7.6)下安装Gitlab详细教程

    Gitlab搭建操作步骤: 1.查看Linux系统版本确认gitlab需要使用的安装包类型 使用命令:cat /etc/redhat-release CentOS Linux release 7.6. ...

  3. Git问题

    1. LF will be replaced by CRLF rm -rf .git // 删除.git git config --global core.autocrlf false //禁用自动转 ...

  4. 使用WIFI网卡 dhcp动态获取IP

    前面几篇博客中,wifi网卡的ip都是手工设置的,本篇博客将来移植dhcp,使得wifi网卡可以动态的获取ip.路由等信息. 那我们去哪里下载dhcp源码呢?在pc机上执行dh +tab键,看一下有哪 ...

  5. USACO Milk Routing

    洛谷 P3063 [USACO12DEC]牛奶的路由Milk Routing 洛谷传送门 JDOJ 2334: USACO 2012 Dec Silver 3.Milk Routing JDOJ传送门 ...

  6. 装ubuntu的坑

    装ubuntu安装盘的U盘,在BOOT中会出现两种载入方式,切记不要用UEFI方式打开,否则安装ubuntu会在最后卡在GRUB的安装上面,然后失败.

  7. Educational Codeforces Round 78 (Rated for Div. 2) D. Segment Tree

    链接: https://codeforces.com/contest/1278/problem/D 题意: As the name of the task implies, you are asked ...

  8. Pandas | 21 日期功能

    日期功能扩展了时间序列,在财务数据分析中起主要作用.在处理日期数据的同时,我们经常会遇到以下情况 - 生成日期序列 将日期序列转换为不同的频率 创建一个日期范围 通过指定周期和频率,使用date.ra ...

  9. mysql 8创建远程访问用户以及连接mysql速度慢的解决方法

      mysql 8创建远程访问用户 [root@demo /]# mysql -u root -p  #登录服务器数据库 Enter password:123xxx >user mysql; & ...

  10. 关于sg函数的一些证明

    复习csp2019的时候稍微看了看博弈论,发现自己对于sg函数的理解完全不到位 有些定义甚至想都没想过 于是就口胡了一篇blog来安慰虚弱的自己 Question 1 对于一个满足拓扑性质的公平组合游 ...