leetcode_865. Smallest Subtree with all the Deepest Nodes
https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/
给定一颗二叉树,输出包含所有最深叶子结点的最小子树的根节点。
解法一:
先使用dfs计算最大深度deep和最大深度的叶子结点数cnt,然后后序遍历,对每个节点再使用dfs计算以该节点为根节点的子树中所含最大深度的节点数,最先找到的最大深度为节点数目等于cnt的节点即为答案。
class Solution{
public:
int deep=-,cnt=;
TreeNode* res=NULL;
TreeNode* subtreeWithAllDeepest(TreeNode* root){
cntdeepest(root, );
backorder(root, );
return res;
}
void cntdeepest(TreeNode * root, int d){
if(d>deep){
deep=d;
cnt=;
}else if(d==deep)
cnt++;
if(root->left != NULL)
cntdeepest(root->left,d+);
if(root->right != NULL)
cntdeepest(root->right, d+);
}
void backorder(TreeNode* root, int d){
if(root == NULL)
return;
if(res != NULL)
return;
backorder(root->left, d+);
backorder(root->right, d+);
int all = calcdeepest(root, d);
if(res==NULL && all == cnt)
res = root;
}
int calcdeepest(TreeNode* root, int d){
if(root == NULL)
return ;
if(d == deep)
return ;
int left=, right=;
if(root->left != NULL)
left = calcdeepest(root->left, d+);
if(root->right != NULL)
right = calcdeepest(root->right, d+);
int all = left+right;
return all;
}
};
这种解法若节点数过多,会比较费时。
解法二:官方题解
dfs(TreeNode* root, depth)返回以root为根节点的子树中包含该子树上所有深度最深叶节点的指针和最大深度。
若为子节点,返回其指针和深度。
若左子树的深度大于右子树,说明只有左子树中包含深度最大的叶节点。
若其中一个子树为空,说明另外一棵子树包含着深度最深的节点。
若两子树深度相同,说明该节点是当前包含该子树上所有深度最深节点的最小子树根节点。
struct Return{
TreeNode* root_;
int depth_;
Return(TreeNode* root, int depth):root_(root),depth_(depth){}
};
class Solution {
public:
TreeNode* subtreeWithAllDeepest(TreeNode* root) {
Return ret = dfs(root, );
return ret.root_;
}
Return dfs(TreeNode* root, int depth){
if(root == NULL)
return Return(NULL, );
if(root->left == NULL && root->right == NULL)
return Return(root, depth);
Return left = dfs(root->left, depth+);
Return right = dfs(root->right,depth+);
if(left.root_ == NULL && right.root_ == NULL) //叶节点
return Return(root, depth);
else if(left.root_ == NULL) //左子树为空的中间节点
return right;
else if(right.root_ == NULL) //右子树为空的中间结点
return left;
else if(left.depth_ == right.depth_)
return Return(root, left.depth_);
else
return left.depth_>right.depth_?left:right;
}
};
leetcode_865. Smallest Subtree with all the Deepest Nodes的更多相关文章
- [LeetCode] Smallest Subtree with all the Deepest Nodes 包含最深结点的最小子树
Given a binary tree rooted at root, the depth of each node is the shortest distance to the root. A n ...
- [Swift]LeetCode865. 具有所有最深结点的最小子树 | Smallest Subtree with all the Deepest Nodes
Given a binary tree rooted at root, the depth of each node is the shortest distance to the root. A n ...
- 865. Smallest Subtree with all the Deepest Nodes 有最深节点的最小子树
[抄题]: Given a binary tree rooted at root, the depth of each node is the shortest distance to the roo ...
- LeetCode 865. Smallest Subtree with all the Deepest Nodes
原题链接在这里:https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 题目: Given a binar ...
- 【LeetCode】865. Smallest Subtree with all the Deepest Nodes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- FB面经 Prepare: LCA of Deepest Nodes in Binary Tree
给一个 二叉树 , 求最深节点的最小公共父节点 . retrun . 先用 recursive , 很快写出来了, 要求用 iterative . 时间不够了... Recursion: 返回的时候返 ...
- LeetCode Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- Windows的MAX_PATH
MAX_PATH的解释: 文件名最长256(ANSI),加上盘符(X:\)3字节,259字节,再加上结束符1字节,共260http://msdn.microsoft.com/en-us/library ...
- SQLite数据库框架--FMDB简单介绍
1.什么是FMDB FMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API 2.FMDB的优点 使用起来更加面向对象,省去了很多麻烦.冗余的C语言代码 对比 ...
- 四:多线程--NSOperation简单介绍
一.NSOperation简介 1.NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQu ...
- 关于python语言使用redis时,连接是否需要关闭的问题
python操作完redis,需要关闭连接的吧,怎么关闭呢 1人赞 回复 君惜丶: redis-server会关闭空闲超时的连接redis.conf中可以设置超时时间:timeout 300 2017 ...
- codeforces round 417 div2 补题 CF 812 A-E
A Sagheer and Crossroads 水题略过(然而被Hack了 以后要更加谨慎) #include<bits/stdc++.h> using namespace std; i ...
- WEB开发框架系列教程 (三)页面功能开发(2)
上一节介绍了,基础资料币别信息的开发,只通过辅助开发工具,创建及资料表,填写 表名,程序就完全好了. 最后也说到,可能我们也会面对另外一些基础资料信息的维护,但是不是简单到只有代码 和名称,可能还有另 ...
- XDCTF2015代码审计全解
此次CTF WEB2是一个大题,一共4个flag,分别代表:获取源码.拿下前台管理.拿下后台.getshell. 目标站:http://xdsec-cms-12023458.xdctf.win/ 根据 ...
- Java知识点脑图
做服务器开发有十几年了,其中大部分用到的都是Java服务器开发,从JDK1.4到现在的JDK1.8,从基本的Java Application到 J2EE(JBOSS,Glassfish),OSGI,到 ...
- 2017 JUST Programming Contest 3.0 I. Move Between Numbers
I. Move Between Numbers time limit per test 2.0 s memory limit per test 256 MB input standard input ...
- Data Center Maintenance CodeForces - 950E
http://codeforces.com/contest/950/problem/E 贴一份板子 #include<cstdio> #include<vector> #inc ...