原题链接在这里:https://leetcode.com/problems/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 node is deepest if it has the largest depth possible among any node in the entire tree.

The subtree of a node is that node, plus the set of all descendants of that node.

Return the node with the largest depth such that it contains all the deepest nodes in its subtree.

Example 1:

Input: [3,5,1,6,2,0,8,null,null,7,4]
Output: [2,7,4]
Explanation: We return the node with value 2, colored in yellow in the diagram.
The nodes colored in blue are the deepest nodes of the tree.
The input "[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]" is a serialization of the given tree.
The output "[2, 7, 4]" is a serialization of the subtree rooted at the node with value 2.
Both the input and output have TreeNode type.

Note:

  • The number of nodes in the tree will be between 1 and 500.
  • The values of each node are unique.

题解:

If both sides have deepest nodes, return root. If only left side has deepest nodes, return left side result. Vice versa.

To check if one side has deepest nodes, calculate the deepest depth of that side.

If left deepest depth == right deepest depth, then both sides have deepest nodes, return root.

If left deepest depth > right deepest depth, then only left side has deepest node, return the result node from left side.

Time Complexity: O(n).

Space: O(h). stack space.

AC Java:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import javafx.util.Pair; class Solution {
public TreeNode subtreeWithAllDeepest(TreeNode root) {
Pair<Integer, TreeNode> res = dfs(root);
return res.getValue();
} private Pair<Integer, TreeNode> dfs(TreeNode root){
if(root == null){
return new Pair(0, null);
} Pair<Integer, TreeNode> left = dfs(root.left);
Pair<Integer, TreeNode> right = dfs(root.right); int lDepth = left.getKey();
int rDepth = right.getKey();
int deepestDepth = Math.max(lDepth, rDepth)+1;
if(lDepth < rDepth){
return new Pair(deepestDepth, right.getValue());
}else if(lDepth > rDepth){
return new Pair(deepestDepth, left.getValue());
}else{
return new Pair(deepestDepth, root);
}
} }

类似Lowest Common Ancestor of a Binary Tree.

LeetCode 865. Smallest Subtree with all the Deepest Nodes的更多相关文章

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

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

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

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

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

  5. leetcode_865. Smallest Subtree with all the Deepest Nodes

    https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 给定一颗二叉树,输出包含所有最深叶子结点的最小子树 ...

  6. [LeetCode] Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  7. Leetcode: Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  8. [LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字

    Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...

  9. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

随机推荐

  1. vip视频会员共享电影免费看

    vip会员在线看是一款可以观看持有会员特权的视频才能看的网站程序,小而强大,目前支持所有视频网站解析! 解析需要时间耐心等待20秒,如果加载失败,请切换接口耐心等待20秒! 找要要看的vip电影地址, ...

  2. Direct Buffer介绍

    Direct Buffer 前言 上篇文章Buffer末尾中谈到堆内Buffer(Heap Buffer)和直接Buffer(Direct Buffer)的概念,但是却一笔带过,并未涉及其细节,这篇文 ...

  3. Vue,Javascript--时间戳的操作

    new Date(parseInt(data.substring(6, data.length - 2))).toLocaleDateString(); 我这里的data记得替换成你的数据,在过滤器中 ...

  4. Windows定时清理文件处理脚本

    一.运行CMD,输入forfile/?,即可获取forfile的使用方法 /P  路径 /M  文件类型 /D  时间   + | -     +:之后    - :之前   example:-2   ...

  5. winform+cefSharp实现窗体加载浏览器

    1:新建winform项目 2:安装cefSharp 3:配置管理器更改为X86 4:添加引用 using CefSharp; using CefSharp.WinForms; 5:项目启动,打开网页 ...

  6. 【转载】C#通过Copy方法快速复制DataTable对象

    C#中的Datatable数据变量的操作过程中,可以通过DataTable的Copy方法快速复制当前的DataTable变量到新对象中,复制数据包含当前DataTable的结构信息如列名,同时也包含当 ...

  7. js new Date()不带时分秒时,时间变了 问题解决

    //先把电脑系统时间的 时区 调到别的时间一下如 夏威夷 UTC-10:00//在浏览器的Console里运行如下代码,getMonth是从0开始的,所以要+1 var d=new Date(&quo ...

  8. div css字间距

    div:nth-child(1){ color: #FFFFFF; font-size: 13px; font-weight: bold; margin-bottom: 25px; letter-sp ...

  9. ffmpeg 详解

    来源:http://blog.itpub.net/9399028/viewspace-1242300/ FFMPEG详解   认识FFMPEG FFMPEG堪称自由软件中最完备的一套多媒体支持库,它几 ...

  10. 5.2 odex文件

    odex是OptimizedDEX的缩写,是优化过的dex文件 odex两种存在方式: 1. 从apk程序中提取,和apk文件放在一起,后缀 odex,此类文件多是AndroidRom系统文件 2.  ...