题目描述:

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.

解题思路:

树一般用递归的方法。

代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode* pruneTree(TreeNode* root) {
13         if (root == NULL)
14             return NULL;
15         root->left = pruneTree(root->left);
16         root->right = pruneTree(root->right);
17         if (root->val == 0 && root->left == NULL && root->right == NULL) {
18             free(root);
19             return NULL;
20         }
21         return root;
22     }
23 };

转载于:https://www.cnblogs.com/gsz-/p/9385599.html

814. Binary Tree Pruning相关推荐

  1. 814. Binary Tree Pruning(C语言)

    814. Binary Tree Pruning(C语言) 深度优先搜索树 + 剪枝 题目 Given the root of a binary tree, return the same tree ...

  2. leetcode 814. Binary Tree Pruning | 814. 二叉树剪枝(Java)

    题目 https://leetcode.com/problems/binary-tree-pruning/ 题解 思路很简单,看草稿: /*** Definition for a binary tre ...

  3. 107. Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  4. 102. Binary Tree Level Order Traversal

    题目 Binary Tree Level Order Traversal 层次遍历二叉树 链接 Given a binary tree, return the level order traversa ...

  5. [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  6. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  7. 二叉树的路径(根节点到叶节点)Binary Tree Paths

    为什么80%的码农都做不了架构师?>>>    问题: Given a binary tree, return all root-to-leaf paths. For example ...

  8. LeetCode - Maximum Depth of Binary Tree

    递归求二叉树的最大深度. /*** Definition for binary tree* public class TreeNode {* int val;* TreeNode left;* Tre ...

  9. Balanced Binary Tree leetcode java

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

最新文章

  1. SVO: 视觉SLAM中特征点法与直接法结合
  2. android 6.0谷歌,Android 6.0来了!谷歌月底要发布Android M系统
  3. C#从服务器下载文件到客户端源码
  4. python3使用serial以及pyserial包读取串口数据并解析字节数组,涉及数据移位以及Python无符号整数转为有符号整数操作
  5. webkit的几个属性
  6. NodeJS的环境变量process.env.*
  7. linux高级命令组合
  8. 透明加密系统设计及实现-绪论
  9. 【个人笔记】OpenCV4 C++ 快速入门 29课
  10. kafka 主从同步入门
  11. 2021-08-3116. 最接近的三数之和 排序+双指针
  12. 计算机总线相关知识,计算机包括哪几种总线?
  13. 嵌入式linux系统开发教程
  14. win7系统关闭445端口批处理脚本
  15. 大数据分析难不难好学吗?
  16. python的江湖世界
  17. ISA 95企业和控制系统集成的框架和分层
  18. IT能力框架(模型)
  19. 软件破解中常用API
  20. 【EXCEL】详解使用python读写EXCEL文件(xlrd,xlwt)

热门文章

  1. 数学之旅-不动点定理
  2. 数据挖掘(10):卷积神经网络算法的一个实现
  3. 笔迹鉴别(5) —— 笔迹判别
  4. LA3602DNA序列
  5. UVA11729突击战(汇报和执行任务)
  6. POJ2553 强连通出度为0的应用
  7. 【RecyclerView】 十四、GridLayoutManager 网格布局管理器 ( GridLayoutManager.SpanSizeLookup 指定 item 元素占用网格个数 )
  8. 【组合数学】排列组合 ( 多重集组合数 | 所有元素重复度大于组合数 | 多重集组合数 推导 1 分割线推导 | 多重集组合数 推导 2 不定方程非负整数解个数推导 )
  9. 【组合数学】鸽巢原理 ( 鸽巢原理简单形式 | 鸽巢原理简单形式示例 1、2、3 )
  10. 【Netty】Netty 核心组件 ( Future | Channel | Selector | ChannelHandler )