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 ...
随机推荐
- python dig 模拟—— DGA域名判定用
#!/usr/bin/env python import dns.resolver, sys def get_domain_ip(domain): """Get the ...
- linux下离线安装svn服务器并配置
一.下载相应的包 subversion-1.8.18.tar.gz 下载地址:http://subversion.apache.orgsqlite-autoconf-3190300.tar.gz ...
- 异或运算(2014西安网络赛H题)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 题意:给出范围N,给出0-N的一个排列a.让你求出另外一个排列b,使 t = a1 ^ b1 + a ...
- 最安全的api接口认证
最安全的api接口认证 实现步骤: 1.客户端与服务器都存放着用于验证的Token字段,客户端在本地把自己的 用户名+时间戳+Token 组合进行MD5加密后生成一段新的md5-token. 2.客户 ...
- 谁动了我的cpu——oprofile使用札记
引言 cpu无端占用高?应用程序响应慢?苦于没有分析的工具? oprofile利用cpu硬件层面提供的性能计数器(performance counter),通过计数采样,帮助我们从进程.函数.代码层面 ...
- 《JAVA与模式》之调停者模式
调停者模式是对象的行为模式.调停者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显引用.从而使它们可以较松散地耦合.当这些对象中的某些对象之间的相互作用发生改变时,不会立即影响到其他的一些 ...
- MFC绘图
//20171/121 两点一线 比如鼠标左击和鼠标弹起的两个消息 然后响应从而获取一条线2 添加响应函数方法 类图->右击->addwindowsmessage3 Dview和main中 ...
- codeforces 28D(dp)
D. Don't fear, DravDe is kind time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 关于Http的小常识(转载,仅供参考)
HTTP请求头提供了关于请求,响应或者其他的发送实体的信息.HTTP的头信息包括通用头.请求头.响应头和实体头四个部分.每个头域由一个域名,冒号(:)和域值三部分组成. 通用头标:即可用于请求,也可用 ...
- poj 3164 Command Network (朱刘算法)
题目链接: http://poj.org/problem?id=3164 题目大意: 有n个点(用坐标表示)各点编号分别为1—>n,m条单向路,问能否存在一个花费价值最小的网络,能使从1点到达任 ...