Description: 
We are given the head node root of a binary tree, where additionally every node’s value is either a 0 or a 1.

Return the same tree where every subtree (of the given tree) not containing a 1 has been removed.

(Recall that the subtree of a node X is X, plus every node that is a descendant of X.)





Note:

  • The binary tree will have at most 100 nodes
  • The value of each node will only be 0 or 1.

问题描述: 
给定二叉树的头节点root,这个二叉树的每个节点值为0或者1. 
现在要对该二叉树剪枝,对节点剪枝的条件为该节点的所有子树都为0

问题分析: 
利用后序遍历将不符合条件的节点删除就可以了。

class Solution {
public TreeNode pruneTree(TreeNode root) {
if(root == null) return null; root.left = pruneTree(root.left);
root.right = pruneTree(root.right); if(root.left == null && root.right == null && root.val == 0) root = null; return root;
}
}

  

814-Binary Tree Pruning的更多相关文章

  1. 814. Binary Tree Pruning(leetcode) (tree traverse)

    https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode tr ...

  2. Leetcode 814. Binary Tree Pruning

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

  3. 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...

  4. [Swift]LeetCode814. 二叉树剪枝 | Binary Tree Pruning

    We are given the head node root of a binary tree, where additionally every node's value is either a ...

  5. [LeetCode] Binary Tree Pruning 二叉树修剪

    We are given the head node root of a binary tree, where additionally every node's value is either a ...

  6. leetcode814 Binary Tree Pruning

    """ We are given the head node root of a binary tree, where additionally every node's ...

  7. LeetCode题解之Binary Tree Pruning

    1.题目描述 2.问题分析 使用递归 3.代码 TreeNode* pruneTree(TreeNode* root) { if (root == NULL) return NULL; prun(ro ...

  8. [Leetcode] Binary Tree Pruning

    題目是說,如果左右子樹都不存在又自已為0,就去掉那個子樹(設為null) recursive後序,左子樹,右子樹,然後是根 自已同時又是別人的子樹,所以要告訢根自已是不是存在 從a開始,左右子樹都不存 ...

  9. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  10. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

随机推荐

  1. XML语言2.约束

    三.XML的约束 目的:编写一个文件用来约束另一个xml文件的书写规范. DTD 用来约束xml的文件格式,保证这个文件是有效的. 内部DTD:所有的约束都是直接定义在xml文件中的 内部的 DOCT ...

  2. Android Studio 真机调试 连接手机

    前提:adb环境已经配置 手机端: 1.打开手机开发者权限,”设置“ 中找到 “版本号”,连续多次点击,会提示打开“开发者”.我的是 “设置” --> "关于手机" --&g ...

  3. 在vue项目中mock数据

    第一步:安装: 在命令行中执行: npm install mockjs; 第二步:定义index.js文件 我们新建一个mock文件夹,此文件夹中建一个index.js文件:在index.js中输入以 ...

  4. C盘突然爆满

    C盘突然爆满!幸好还开的机!~~ 因为是突然就爆满了,想着应该是虚拟内存的原因!于是就开始了探索.... 1.文件夹选项中把所有文件都显示出来. 2.在C盘你就会看到一个“pagefile.sys”的 ...

  5. Python-字典与json的转换

    #json是字符串,只不过长得像字典 import json user_info='''{"niuhy":1234,"shanbl":44566}''' #js ...

  6. 企业IT管理员IE11升级指南【11】—— 通过SCCM 2012和WSUS部署Internet Explorer 11

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  7. springboot加ES实现全局检索

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...

  8. freekan5.9电影网站安装及源码分享

    Freekan是一套目前非常火的电影网站系统,全自动采集,支持对接公众号 服务器环境:centos7,宝塔面板,php7.1(重要),nignx,mysql 1.首先上传压缩包到网站目录,然后解压 2 ...

  9. toastr操作完成提示框

    toastr.js组件 关于信息提示框,项目中使用的是toastr.js这个组件,这个组件最大的好处就是异步.无阻塞,提示后可设置消失时间,并且可以将消息提示放到界面的各个地方. 官方文档以及源码 源 ...

  10. 【从零开始搭建自己的.NET Core Api框架】(六)泛型仓储的作用

    系列目录 一.  创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...