原题链接在这里: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. ubuntn18 docker zabbix+grafana安装和使用

    在ubuntu docker inflxudb(安装 使用 备份 还原 以及python编码) telegraf Grafana我采用telegraf采集数据, 本文计划采用zabbix来才采集数据, ...

  2. 【题解】Luogu P3349 [ZJOI2016]小星星

    原题传送门 我们考虑设\(dp_{i,j}\)表示树上的点\(i\)在图上对应的点为\(j\)时\(i\)和子树对应在图上的方案数 \(dp_{u_i}=\prod_{v \in u.son} dp_ ...

  3. 『Broken Robot 后效性dp 高斯消元』

    Broken Robot Description 你作为礼物收到一个非常聪明的机器人走在矩形板上.不幸的是,你明白它已经破碎并且行为相当奇怪(随机).该板由N行和M列单元组成.机器人最初位于第i行和第 ...

  4. TestNG系列(三)TestNG之XML文件配置

    前言 上一篇博客说了TestNG的注解,这篇博客来介绍Test.xml文件. Test.xml文件可以更方便的管理和执行测试用例 一.Test.xml-suite: suite为Test.xml的根节 ...

  5. latex制作表格-跨行跨列

    1.列的合并,使用 \multicolumn{跨几列}{格式}{填充内容} \documentclass[UTF8]{ctexart} \begin{document} 三囚犯问题进行300次实验后\ ...

  6. 踏入OpenGL大门 —— VS2015开发环境配置 (详细图文)

    转自: https://www.jianshu.com/p/68c314fa9fea?from=groupmessage   眼睛熊 ---------------- 本文 ------------- ...

  7. .net Core 解决Form value count limit 1024 exceeded. (文件上传过大)

    异常清空如图 原因:.net core提交的表单限制太小导致页面表单提交失败 在控制器上使用 RequestFormLimits attribute [RequestFormLimits(ValueC ...

  8. springboot整合docker部署

    环境安装 首先,需要安装Docker(例如:docker for windows) 下载地址:https://download.docker.com/win/stable/Docker%20for%2 ...

  9. 第三章 Maven构建 Java Spring Boot Web项目

    3.1   认识Srping Boot Spring Boot是一个框架,是一种全新的编程规范,它的产生简化了对框架的使用,简化了Spring众多的框架中大量的繁琐的配置文件,所以说Spring Bo ...

  10. java中创建线程的3种方法

    1.继承Thread类优点:可以直接使用Thread类中的方法,代码比较简单.缺点:继承Thread类之后不能继承其他类. 2.实现Runable接口优点:实现接口,比影响继承其他类或实现接口.缺点: ...