LeetCode 865. Smallest Subtree with all the Deepest Nodes
原题链接在这里: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的更多相关文章
- 【LeetCode】865. Smallest Subtree with all the Deepest Nodes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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] 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 ...
- leetcode_865. Smallest Subtree with all the Deepest Nodes
https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 给定一颗二叉树,输出包含所有最深叶子结点的最小子树 ...
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- Leetcode: Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [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 ...
- [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 ...
随机推荐
- .netcore docker noe4j
1.借用docker搭建noe4j环境 docker pull neo4j docker run -d --restart=always -p7474: -p7687: -v /root/docker ...
- FusionInsight大数据开发---sorl应用开发
sorl应用开发 要求: 了解Solr应用开发适用场景 熟悉Solr应用开发流程 熟悉并使用Solr常用API 理解Collection设计基本原则 应用开发实践 Solr简介 Solr是一个高性能, ...
- 订单BOM与销售BOM的区别
訂單BOM: 是實際生產時用的BOM, 在標準BOM和銷售BOM基礎上增減物料的BOM 銷售BOM: 是為特定客戶設定的BOM, 在主檔數據層次上的BOM, 在生產時是帶到訂單BOM中去的. 標準BO ...
- Spring IOC 概述
Spring IOC 概述 IOC(Inversion of Control) 控制反转,也叫 DI(D_ependency injection_) 依赖注入.是一种设计思想.不过我并不同意所谓反转的 ...
- 经实验验证,修正对using namespace std的认识
备注①:name:符号.指的实体包括:变量.函数.类 备注②:认为全局命名空间也是一个包,在此称作 ROOT:: 或 global:: (这样就有了两个特别的包:一个是全局包,一个是std包.但对于编 ...
- 一. jmeter
1.性能测试概述 1.1 主要方向是测试系统在一定负荷压力下,系统的响应时间,吞吐量,稳定性,系统的可扩展性等性能指标. 结合应用的架构和实现细节找出问题,并最终确认问题得到解决的过程. 目的: 1. ...
- PHP-FPM的相关知识的深度解释
一.需要搞清楚几个名词概念 1. CGI(Common Gateway Interface,CGI)通用网关接口, 是Web 服务器运行时外部程序的规范,按CGI 编写的程序可以扩展 ...
- 2019 吉比特java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.吉比特等公司offer,岗位是Java后端开发,因为发展原因最终选择去了吉比特,入职一年时间了,也成为了面试官 ...
- 前端1-----HTML了解,内联标签(图片,超链接锚点,超链接邮箱)
前端1-----HTML了解,内联标签(图片,超链接锚点,超链接邮箱) 一丶自定制B/S # -*-coding:utf-8-*- # Author:Ds import socket IP_PORT= ...
- react,react-router,redux+react-redux 构建一个React Demo
创建初始化应用 加速我们的npm. npm install -g cnpm --registry=https://registry.npm.taobao.org 利用create-react-app ...
We return the node with value 2, colored in yellow in the diagram.