814. Binary Tree Pruning(C语言)
814. Binary Tree Pruning(C语言)
深度优先搜索树 + 剪枝
题目
Given the root of a binary tree, return the same tree where every subtree (of the given tree) not containing a 1 has been removed.
A subtree of a node node is node plus every node that is a descendant of node.
Example 1:
Input: root = [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: root = [1,0,1,0,0,0,1]
Output: [1,null,1,null,1]
Example 3:
Input: root = [1,1,0,1,1,0,1,0]
Output: [1,1,0,1,1,null,1]
Constraints:
The number of nodes in the tree is in the range [1, 200].
Node.val is either 0 or 1.
解答
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/
bool Tra(struct TreeNode* root)
{if(root == NULL){return false;}bool left_flag = Tra(root->left);bool right_flag = Tra(root->right);if(left_flag == false)root->left = NULL;if(right_flag == false)root->right = NULL;if(left_flag == false && right_flag == false && root->val == 0)return false;elsereturn true;
}
struct TreeNode* pruneTree(struct TreeNode* root){bool flag = Tra(root);if(flag == false && root->val == 0)root = NULL;return root;
}
814. Binary Tree Pruning(C语言)相关推荐
- 814. Binary Tree Pruning
题目描述: We are given the head node root of a binary tree, where additionally every node's value is eit ...
- leetcode 814. Binary Tree Pruning | 814. 二叉树剪枝(Java)
题目 https://leetcode.com/problems/binary-tree-pruning/ 题解 思路很简单,看草稿: /*** Definition for a binary tre ...
- C语言实现线索二叉树Threaded Binary Tree (附完整源码)
C语言实现线索二叉树Threaded Binary Tree 树节点定义 实现以下7个接口 完整实现和main测试源码 树节点定义 typedef struct Node {int data; /** ...
- c++ 结构体遍历_二叉树(Binary Tree)的建立与遍历——C语言实现
一.运行环境简介 编辑器:VSCode + MicroSoft原生插件; :cat::dragon:运行环境: MinGW ; :cat::bust_in_silhouette:常用指令: gcc ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)
目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...
- [LeetCode] Binary Tree Paths - 二叉树基础系列题目
目录: 1.Binary Tree Paths - 求二叉树路径 2.Same Tree - 判断二叉树相等 3.Symmetric Tree - 判断二叉树对称镜像 Binary Tree Path ...
- 编程基础 - 线索二叉树 (Threaded Binary Tree)
编程基础 - 线索二叉树 (Threaded Binary Tree) 返回分类:全部文章 >> 基础知识 返回上级:编程基础 - 二叉树 (Binary Tree) 本文将介绍线索二叉树 ...
- EssentialC++_chapter06 binary tree 的学习笔记记录及问题
Essential C++ chapter06 binary tree 的学习笔记记录及问题 Author:Dargon Note date:2021/02/28 Source: <Essent ...
最新文章
- 西工大开源拥挤人群数据集生成工具,大幅提升算法精度 | CVPR 2019
- 《iOS 6高级开发手册(第4版)》——1.5节秘诀:使用加速能力“向上”定位
- vue组件实现查看大图效果
- 【Java】集合+I/O流+多线程の练习题+面试题
- 安卓--selector简单使用
- wpf datagrid 计算两个列的差值_天体到达指定方位角的时间计算
- 【学习 Opencv】—— 常见接口
- oracle服务器客户端配置文件,服务器 oracle 客户端配置文件
- java itext 里表格_Java解析PDF里的表格内容
- 无人车传感器 GPS 深入剖析
- python快乐数字怎么表达_Python中的快乐数字
- Android---AndroidX
- 百度2012实习生校园招聘笔试题
- HOW2J.CN--JAVA学习笔记
- python自动化测试项目全流程
- Qt linux获取cpu使用率、内存、网络收发速度、磁盘读写速度、磁盘剩余空间等
- Java线程间的通信方式
- Android图片资源处理
- 微信诱导关注php源码,微信公众平台关于在线网页制作类诱导关注的公告
- Unity塔防游戏的制作与实现