LeetCode题解之Binary Tree Pruning
1、题目描述

2、问题分析
使用递归
3、代码
TreeNode* pruneTree(TreeNode* root) {
if (root == NULL)
return NULL;
prun(root);
return root;
}
void prun(TreeNode *root)
{
if (root == NULL)
return;
if (!has_ones(root->left))
root->left = NULL;
if (!has_ones(root->right))
root->right = NULL;
prun(root->left);
prun(root->right);
}
bool has_ones(TreeNode *t)
{
if (t == NULL)
return false;
return (t->val == ) || has_ones(t->left) || has_ones(t->right);
}
LeetCode题解之Binary Tree Pruning的更多相关文章
- 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...
- leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- [LeetCode 题解]: Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- LeetCode题解之Binary Tree Right Side View
1.题目描述 2.问题分析 使用层序遍历 3.代码 vector<int> v; vector<int> rightSideView(TreeNode* root) { if ...
- LeetCode题解Maximum Binary Tree
1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) ...
- LeetCode题解之Binary Tree Paths
1.题目描述 2.分析 使用递归. 3.代码 vector<string> ans; vector<string> binaryTreePaths(TreeNode* root ...
- LeetCode题解之Binary Tree Level Order Traversal II
1.题目描述 2.题目分析 先遍历,再反转. 3.代码 vector<vector<int>> levelOrderBottom(TreeNode* root) { vecto ...
- LeetCode题解之Binary Tree Tilt
1.题目描述 2.分析 利用递归实现. 3.代码 int findTilt(TreeNode* root) { if (root == NULL) ; ; nodesTilt(root,ans); r ...
随机推荐
- LeetCode:114_Flatten Binary Tree to Linked List | 将一棵二叉树变成链表的形式 | Medium
要求:Given a binary tree, flatten it to a linked list in-place.将二叉树转化为平坦序列的树.比如: 结题思路: 该题有个提示,转化后的树的序列 ...
- PyTorch(一)Basics
PyTorch Basics import torch import torchvision import torch.nn as nn import numpy as np import torch ...
- [EXP]K8 jboss invoke deploy getshell exploit
MSF jboss invoke deploy EXPLOIT moudle Date:2013.11.28 Author:K8gege 改这个EXP中 过程有点不进人意 没能在一个payload里直 ...
- hdu 6086 -- Rikka with String(AC自动机 + 状压DP)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- Linux学习笔记之八————vim编辑器常用命令总结
<1>从命令行模式到插入模式 i :在光标前插入 a :在光标后插入 I :在光标所处在的行的行首 A :在光标所处在的行的末尾 o :在光标所处在的行的下一行 行首 O :在光标所 ...
- 在Hadoop集群上的Hive配置
1. 系统环境Oracle VM VirtualBoxUbuntu 16.04Hadoop 2.7.4Java 1.8.0_111 hadoop集群master:192.168.19.128slave ...
- [java初探09]__关于java的包装类
前言 在Java语言的学习过程中,我们逐渐的理解了Java面向对象的思想,与类和对象的应用.但是在基本数据类型的使用上,我们无法将其定义为一个对象,通过使用对象的方法来使用它们,但是Java语言的思想 ...
- SQL 必知必会·笔记<9>使用子查询
子查询(subquery),即嵌套在其他查询中的查询. 1. 利用子查询进行过滤 SELECT 语句中,子查询总是从内向外处理.示例: SELECT cust_name, cust_contact F ...
- Debug技巧
多线程调试 有些时候为了观察多个线程间变量的不同状态,以及锁的获取等,就会想到在代码里加个断点debug一下. 在IDE里断点停下来的时候,可以切换到另外的线程中,跑其他的代码,不会互相影响.这里是有 ...
- Java并发编程笔记之 CountDownLatch闭锁的源码分析
JUC 中倒数计数器 CountDownLatch 的使用与原理分析,当需要等待多个线程执行完毕后在做一件事情时候 CountDownLatch 是比调用线程的 join 方法更好的选择,CountD ...