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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. LeetCode 865. Smallest Subtree with all the Deepest Nodes

    原题链接在这里:https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 题目: Given a binar ...

  5. 【LeetCode】865. Smallest Subtree with all the Deepest Nodes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. FB面经 Prepare: LCA of Deepest Nodes in Binary Tree

    给一个 二叉树 , 求最深节点的最小公共父节点 . retrun . 先用 recursive , 很快写出来了, 要求用 iterative . 时间不够了... Recursion: 返回的时候返 ...

  7. LeetCode Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. ABAP 检查全角半角

    check全角or半角的方法 第一种方法SJIS_DBC_TO_SBC 全角转半角 SJIS_SBC_TO_DBC 半角转换为全角 设定 import all =xtext = 文本全角-〉半角,返回 ...

  2. Lightoj 1009 - Back to Underworld

    1009 - Back to Underworld    PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 32 ...

  3. [RK3288][Android6.0] 关于uboot中logo相关知识点小结【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/76256224 Platform: Rockchip OS: Android 6.0 Kern ...

  4. 唯一性校验 impl 和 action

    IMPL方法实现 //  这个方法是通过ID修改数据 如果得到结果大于0 表明结果有重复 如果得到结果小于0 表明结果正确 @Override public boolean checkVersion( ...

  5. mac中的本地项目和登陆的钥匙串怎么解锁

    前提条件:电脑语言换成英语,然后按下面的操作就可以了. 1.打开launchapd中的钥匙串访问 2 选中第一栏登录, 点击左上角的锁锁上, 再点一次解锁. 3. 这回会弹出个东西 点击如上图中的重密 ...

  6. python 模块 module 规范

    # /usr/bin/python # -*- coding=utf-8 -*- """This is a standard module""&quo ...

  7. tyvj 1666 城市建设【最小生成树】

    -Wall是个好东西,要不然我至死都看不出来我把(b[i]+b[j])写成了(b[i],b[j])-- 还是来自lyd的题解: (其实原来课件第一行式子写错了没有-1,然而我用sai手画了一个上去hh ...

  8. CodeForces E. Goods transportation【最大流+dp最小割】

    妙啊 首先暴力建图跑最大流非常简单,s向每个i连流量为p[i]的边,每个i向t连流量为s[i]的边,每个i向j连流量为c的边(i<j),但是会又T又M 考虑最大流=最小割 然后dp求最小割,设f ...

  9. bzoj 3205: [Apio2013]机器人【dfs+斯坦纳树+spfa】

    第一次听说斯坦纳树这种东西 先dfs预处理出来dis[i][j][k]表示格子(i,j)向k方向转移能到哪,记忆话搜索预处理,注意如果有环的话特判一下 设f[i][j][x][y]表示复合机器人i-j ...

  10. 第二篇(那些JAVA程序BUG中的常见单词)

    Cannot instantiate the type xxx 无法实例化类型xxx instantiate 实例化