博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode#递归#/814. 二叉树剪枝
阅读量:3952 次
发布时间:2019-05-24

本文共 1138 字,大约阅读时间需要 3 分钟。

给定二叉树根结点 root ,此外树的每个结点的值要么是 0,要么是 1。

返回移除了所有不包含 1 的子树的原二叉树。

( 节点 X 的子树为 X 本身,以及所有 X 的后代。)

示例1:

输入: [1,null,0,0,1]
输出: [1,null,0,null,1]

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode() {} *     TreeNode(int val) { this.val = val; } *     TreeNode(int val, TreeNode left, TreeNode right) { *         this.val = val; *         this.left = left; *         this.right = right; *     } * } */class Solution {
public TreeNode pruneTree(TreeNode root) {
//移除子树全部都是 0 的树 if(root == null) return null; return new Tree(root).get(); } static class Tree{
TreeNode root; Tree(TreeNode root) {
this.root = root;} TreeNode get() {
return get(root); } TreeNode get(TreeNode root) {
if(root == null) return root; root.left = get(root.left); root.right = get(root.right); if(root.left == null && root.right == null && root.val ==0) {
return null; } return root; } }}

转载地址:http://xzyzi.baihongyu.com/

你可能感兴趣的文章
技术道德
查看>>
“需求为王”才是根本
查看>>
高效率的危害
查看>>
寻找边缘性创新
查看>>
让创意瞄准市场
查看>>
高效经理人应具有的八个重要习惯
查看>>
优秀的领导者能读懂人才
查看>>
大智若愚也是领导力
查看>>
android如何编译MTK的模拟器
查看>>
android如何添加AP中要使用的第三方JAR文件
查看>>
利用sudo命令为Ubuntu分配管理权限
查看>>
Ubuntu下几个重要apt-get命令用法与加速UBUNTU
查看>>
Ubuntu中网页各种插件安装命令
查看>>
使用tar命令备份Ubuntu系统
查看>>
ubuntu flash 文字乱码解决方案
查看>>
在ubuntu中运行exe文件
查看>>
ubuntu安装命令
查看>>
和上司沟通必备8个黄金句
查看>>
联系查看两张卡的未接电话记录
查看>>
Python 3 之多线程研究
查看>>