[Leetcode] Binary Tree Pruning

題目是說,如果左右子樹都不存在又自已為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的更多相关文章
- [LeetCode] Binary Tree Pruning 二叉树修剪
We are given the head node root of a binary tree, where additionally every node's value is either a ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- 814. Binary Tree Pruning(leetcode) (tree traverse)
https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode tr ...
- 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...
- Leetcode 814. Binary Tree Pruning
dfs 要点是这一句: return node.val==1 or node.left or node.right 完整代码: # Definition for a binary tree node. ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- 交叉编译支持SVE ACLE的gcc
最近在学习AArch64的SVE技术时,发现目前可以在网上找到的gcc版本都不支持SVE intrinsic方式调用,在看文档时发现,GCC要到2020年的GCC10时才会支持: 在github上看到 ...
- springboot easypoi 报错The bean 'beanNameViewResolver', defined in class path resource [cn/afterturn/e
事故现场: The bean 'beanNameViewResolver', defined in class path resource [cn/afterturn/easypoi/configur ...
- 复数基础及其2D空间的旋转
本文我们讨论复数及其旋转的含义.复数很有意思,本文介绍了复数的基本定义和性质,以及它关于旋转的几何意义. 复数对于旋转的两个方面极为重要: 1. 它引入了旋转算子(rotational operato ...
- Comet OJ - Contest #10 C.鱼跃龙门
传送门 题意: 求最小的\(x\),满足\(\frac{x(x+1)}{2}\% n=0,n\leq 10^{12}\). 多组数据,\(T\leq 100\). 思路: 直接考虑模运算似乎涉及到二次 ...
- django orm 基于双下划线的跨表查询
一..基于双下划线的跨表查询(join实现) key:正向查询按字段,反向查询按表明小写 1.一对多跨表查询 查询在跨表中可以有两种方式,正向查询就是关键字段在你要搜索的表,没有关键字段就是反向查询 ...
- 从Oop-Klass模型看透反射
<红楼梦>第十二回,贾瑞因痴迷王熙凤,被王熙凤折腾的眼看就快不行了.当然这里面是没有多少爱的,完全因王熙凤的美貌而起.就在这时来了一个跛足道人,带来了一面宝镜,说能治好贾瑞的病.当然这可不 ...
- 【CSP-S膜你考】不怕噩梦 (模拟)
不怕噩梦 题面 蚊子最近经常做噩梦,然后就会被吓醒.这可不好.. 疯子一直在发愁,然后突然有一天,他发现蚊子其实就是害怕某些事. 如果那些事出现在她的梦里,就会害怕. 我们可以假定那个害怕的事其实是一 ...
- 【BZOJ4722】由乃
[BZOJ4722]由乃 题面 bzoj 题解 考虑到区间长度为\(14\)时子集个数\(2^{14}>14\times 1000\),由抽屉原理,区间长度最多为\(13\)(长度大于这个值就一 ...
- pytest 打印调试信息
pytest_lean2.py #coding=utf- import pytest import os import sys import time import json sys.path.app ...
- mysql 包含查找
#从表iot_company选择,company_name字段包含10091015的项SELECT id FROM iot_company WHERE company_name LIKE " ...