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 ...
随机推荐
- jquery中一些容易忽略的方法
jquery.ajaxPrefilter:([dataTypes],handle(options,originalOptions,jqXHR)):在发送请求之前和执行$.ajax之前操作options ...
- 为ios app添加广告条
1.广告简介 2.实现步骤: 1>.添加 iAd.framework 框架 2,使用storyboard 运行结果: 2>添加 ADBannerView 视图,并设置代理方法 3>思 ...
- 2018OKR年中回顾 转
一.2018OKR规划 目标1.温习专业基础知识 关键结果1.1 阅读<微积分之屠龙宝刀>+<微积分之倚天宝剑>,加深理解高等数学微积分的各种概念与公式(0.2 屠龙宝刀看了三 ...
- bzoj 4571 美味 —— 主席树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4571 区间找异或值最大,还带加法,可以用主席树: 可以按位考虑,然后通过加上之前已经有的答案 ...
- 将Mozilla ThunderBird最小化到系统托盘(转载)
转自:http://be-evil.org/mozilla-thunderbird-minize-to-tray.html Mozilla ThunderBird是一款非常不错的邮件客户端,但是其在默 ...
- 《windows核心编程系列》七谈谈用户模式下的线程同步
用户模式下的线程同步 系统中的线程必须访问系统资源,如堆.串口.文件.窗口以及其他资源.如果一个线程独占了对某个资源的访问,其他线程就无法完成工作.我们也必须限制线程在任何时刻都能访问任何资源.比如在 ...
- LBP特征 学习笔记
这几天一直在做人脸识别的项目,有用到LBP特征,但是毫无头绪,师姐这几天也比较忙,没有时间来指导我,随自己找相应的介绍LBP的博文来看,现在总算有了一个大体的思路了,就写下来吧 注:参考博文: 目标检 ...
- poj 3253 Fence Repair (水哈夫曼树)
题目链接: http://poj.org/problem?id=3253 题目大意: 有一根木棍,需要截成n节,每节都有固定的长度,一根长度为x的木棒结成两段,需要花费为x,问截成需要的状态需要最小的 ...
- 题解报告:hdu 1576 A/B(exgcd、乘法逆元+整数快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n ...
- 链表中用标兵结点简化代码 分类: c/c++ 2014-09-29 23:10 475人阅读 评论(0) 收藏
标兵结点(头结点)是在链表中的第一个结点,不存放数据,仅仅是个标记 利用标兵结点可以简化代码.下面实现双向链表中的按值删除元素的函数,分别实现 带标兵结点和不带标兵结点两版本,对比可见标兵结点的好处. ...