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语言)相关推荐

  1. 814. Binary Tree Pruning

    题目描述: We are given the head node root of a binary tree, where additionally every node's value is eit ...

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

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

  3. C语言实现线索二叉树Threaded Binary Tree (附完整源码)

    C语言实现线索二叉树Threaded Binary Tree 树节点定义 实现以下7个接口 完整实现和main测试源码 树节点定义 typedef struct Node {int data; /** ...

  4. c++ 结构体遍历_二叉树(Binary Tree)的建立与遍历——C语言实现

    一.运行环境简介 编辑器:VSCode + MicroSoft原生插件; :cat:‍:dragon:运行环境: MinGW ; :cat:‍:bust_in_silhouette:常用指令: gcc ...

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

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

  6. [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

    目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...

  7. [LeetCode] Binary Tree Paths - 二叉树基础系列题目

    目录: 1.Binary Tree Paths - 求二叉树路径 2.Same Tree - 判断二叉树相等 3.Symmetric Tree - 判断二叉树对称镜像 Binary Tree Path ...

  8. 编程基础 - 线索二叉树 (Threaded Binary Tree)

    编程基础 - 线索二叉树 (Threaded Binary Tree) 返回分类:全部文章 >> 基础知识 返回上级:编程基础 - 二叉树 (Binary Tree) 本文将介绍线索二叉树 ...

  9. EssentialC++_chapter06 binary tree 的学习笔记记录及问题

    Essential C++ chapter06 binary tree 的学习笔记记录及问题 Author:Dargon Note date:2021/02/28 Source: <Essent ...

最新文章

  1. 西工大开源拥挤人群数据集生成工具,大幅提升算法精度 | CVPR 2019
  2. 《iOS 6高级开发手册(第4版)》——1.5节秘诀:使用加速能力“向上”定位
  3. vue组件实现查看大图效果
  4. 【Java】集合+I/O流+多线程の练习题+面试题
  5. 安卓--selector简单使用
  6. wpf datagrid 计算两个列的差值_天体到达指定方位角的时间计算
  7. 【学习 Opencv】—— 常见接口
  8. oracle服务器客户端配置文件,服务器 oracle 客户端配置文件
  9. java itext 里表格_Java解析PDF里的表格内容
  10. 无人车传感器 GPS 深入剖析
  11. python快乐数字怎么表达_Python中的快乐数字
  12. Android---AndroidX
  13. 百度2012实习生校园招聘笔试题
  14. HOW2J.CN--JAVA学习笔记
  15. python自动化测试项目全流程
  16. Qt linux获取cpu使用率、内存、网络收发速度、磁盘读写速度、磁盘剩余空间等
  17. Java线程间的通信方式
  18. Android图片资源处理
  19. 微信诱导关注php源码,微信公众平台关于在线网页制作类诱导关注的公告
  20. Unity塔防游戏的制作与实现

热门文章

  1. php单链表检测有没有环,PHP找出链表中环入口节点步骤详解
  2. JAVA校招基础面试题
  3. splay的一些操作
  4. 计算机专业ppt答辩范文,计算机科学与技术专业论文答辩范例.ppt
  5. 树莓派之老的方式重刷Raspbian系统-2015
  6. 动漫里的op、ed、OVA、ost、bl、gl代表什么意思
  7. mosquitto接口流程图
  8. VS2008安装SP1补丁后智能提示从中文变为英文的解决办法
  9. 简单的网页登录注册页面
  10. matlab 函数 平移,MATLAB图线先下平移