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. css3动画应用-音乐唱片旋转播放特效

    css3动画应用-音乐唱片旋转播放特效 核心点: 1.设置图片为圆形居中,使图片一直不停旋转. 2.文字标题(潘玮柏--反转地球)一直从左到右不停循环移动. 3.点击图标,音乐暂停,图片停止旋转:点击 ...

  2. 利用JS 阻止表单提交

    情景一:不存在Ajax异步操作 1 使用背景:会议室预定管理系统中,当表单提交的时候需要验证预约的时间是否符合预定规则(不需要通过访问服务器),否则提示错误信息,阻止表单提交. 2 相关技术点: fo ...

  3. bzoj3330: [BeiJing2013]分数

    口胡 题目hint都给你是一个三分函数了 还不会上三分套三分吗 exp函数又卡 精度又卡 什么sb毒瘤题 浪费时间

  4. LOJ114_k 大异或和_线性基

    LOJ114_k 大异或和_线性基 先一个一个插入到线性基中,然后高斯消元. 求第K小就是对K的每一位是1的都用对应的线性基的一行异或起来即可. 但是线性基不包含0的情况,因此不能确定能否组成0,需要 ...

  5. hdu 1348【凸包模板】

    #include<iostream> #include<iostream> #include<algorithm> #include<cmath> us ...

  6. Django 源码安装及使用

    首先我们使用的是最新版的CentOS系统:CentOS 7.4 在安装django之前,我们首先保证系统中已经安装好setuptools Django安装: 1.首先我们在Django官网下载最新版本 ...

  7. CF1119F Niyaz and Small Degrees

    题意 给你\(n\)个点的树,边有边权 问使得所有的点度数都小于等于\(x\)的最小删边的代价 \([x \in 0...n-1]\) 题解 首先对于每个\(x\) 可以有一个\(O(nlogn)\) ...

  8. 浅谈算法——线段树之Lazy标记

    一.前言 前面我们已经知道线段树能够进行单点修改和区间查询操作(基本线段树).那么如果需要修改的是一个区间该怎么办呢?如果是暴力修改到叶子节点,复杂度即为\(O(nlog n)\),显然是十分不优秀的 ...

  9. [GZOI2016] 亚索的量子实验【分块】

    第二题 亚索的粒子实验 [问题描述] 亚索是一名伟大的科学家,他最近在做一个粒子的实验,粒子初始有一定的能量,实验过程中倘若第i个粒子被注入k能量,那该粒子就会增加k能量,同时由于辐射作用,第2i,3 ...

  10. Sign on Fence CodeForces - 484E

    http://codeforces.com/problemset/problem/484/E 题意: 给定一个长度为n的数列,有m次询问,询问形如l r k 要你在区间[l,r]内选一个长度为k的区间 ...