Binary Tree Pruning

原创 2018年04月15日 21:47:06

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.)

Example 1:
Input: [1,null,0,0,1]
Output: [1,null,0,null,1]
 
Explanation: 
Only the red nodes satisfy the property "every subtree not containing a 1".
The diagram on the right represents the answer.

Example 2:
Input: [1,0,1,0,0,0,1]
Output: [1,null,1,null,1]


Example 3:
Input: [1,1,0,1,1,0,1,0]
Output: [1,1,0,1,1,null,1]


Note:

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

题目描述:给定一颗二叉树,节点的值非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)
    	   return null;
       return root;
   }
}

thinkphp5微信开发实践教程

-
  • 1970年01月01日 08:00

814-Binary Tree Pruning

Description: We are given the head node root of a binary tree, where additionally every node’s valu...
  • LaputaFallen
  • LaputaFallen
  • 2018-04-08 22:15:20
  • 29

Binary Tree Pruning

We are given the head node root of a binary tree, where additionally every node's value is either a ...
  • m0_37889928
  • m0_37889928
  • 2018-04-15 21:47:06
  • 8

【LeetCode】728. Self Dividing Numbers 解题报告

【LeetCode】728. Self Dividing Numbers 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2018-01-13 19:03:03
  • 142

【LeetCode】Longest Word in Dictionary through Deleting 解题报告

【LeetCode】Longest Word in Dictionary through Deleting 解题报告标签(空格分隔): LeetCode题目地址:https://leetcode.co...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2017-04-07 14:51:47
  • 667

【Android开发】问答机器人,聊天类App的开发制作过程记录

缘起很久没写项目了,所以单纯的想练练手,正好看到有问答机器人的接口,想到之前也做过聊天项目,为什么不实验一下呢。当然也是简单调用接口的项目,并没有真正的完成问答的算法等等。业余项目,功能不齐全,只实现...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2017-01-10 17:30:23
  • 1350

【LeetCode】733. Flood Fill 解题报告(Python)

【LeetCode】733. Flood Fill 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/flo...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2018-02-28 15:56:19
  • 84

【机器学习】k-近邻算法(kNN) 学习笔记

【机器学习】k-近邻算法(kNN) 学习笔记标签(空格分隔): 机器学习kNN简介kNN算法是做分类问题的。思想如下:KNN算法的思想总结一下:就是在训练集中数据和标签已知的情况下,输入测试数据,将测...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2017-04-03 19:31:46
  • 1654

【LeetCode】Hamming Distance 解题报告(java & python)

【LeetCode】Hamming Distance 解题报告(java & python) [LeetCode] https://leetcode.com/problems/...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2017-01-06 15:23:46
  • 580

【LeetCode】443. String Compression 解题报告

【LeetCode】443. String Compression 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/str...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2018-01-27 21:11:12
  • 190
收藏助手
不良信息举报
您举报文章:Binary Tree Pruning
举报原因:
原因补充:

(最多只允许输入30个字)