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 ...
随机推荐
- Markdown_word_chain_test2222
# Flutter ![Flutter logo][] [![Gitter Channel][]][Gitter badge] [![Build Status - Cirrus][]][Build ...
- Prometheus 基于文件的服务发现
Prometheus 基于文件的服务发现 官方文档:https://github.com/prometheus/prometheus/tree/master/discovery 服务发现支持: end ...
- 记:使用IScroll.js 开发picker日历组件遇到的问题及经验总结
IScroll中文文档 第一个问题: 边界留白 就是这种,上边界(最小),下边界(最大)有两个列表的位置是不能选择的.解决的办法是: 在HTML中,添加空白节点就行了. 第二个问题:初始化之后的滚动停 ...
- php for循环a到z
首先先介绍2个php内置函数 ord(string):函数返回字符串的首个字符的 ASCII 值.//string:必需.要从中获得 ASCII 值的字符串. chr(ascll): 函数从指定的 A ...
- Tigase XMPP Server
Tigase XMPP Server是我们的旗舰服务器端软件,提供XMPP服务或实例通信(IC)服务.最基本的解释是Tigase是一个聊天服务器,但它远不止于此.聊天是其可能的应用程序之一,但任何类型 ...
- Netty高性能原理和框架架构解析
1.引言 Netty 是一个广受欢迎的异步事件驱动的Java开源网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端. 本文基于 Netty 4.1 展开介绍相关理论模型,使用场景,基本组件 ...
- Linux中的defunct进程(僵尸进程)
一.什么是defunct进程(僵尸进程)?在 Linux 系统中,一个进程结束了,但是他的父进程没有等待(调用wait / waitpid)他,那么他将变成一个僵尸进程.当用ps命令观察进程的执行状态 ...
- 【转载】C#通过遍历DataTable的列获取所有列名
在C#中的Datatable数据变量的操作过程中,可以通过遍历DataTable的所有列对象Columns属性,来获取DataTable中的所有列名信息,DataTable中所有列的对象信息都存储在D ...
- Django下JWT的使用
前言 JWT 是 json web token 的缩写, token的作用你应该已经了解,用于识别用户身份避免每次请求都需要验证 用来解决前后端分离时的用户身份验证 在传统的web项目中 我们会在fo ...
- Miniconda安装 虚拟环境创建 与包管理
安装python 之前安装python包,导致了python里面的包不兼容,用管理工具卸载也下载不掉,重新安装也安装不上,没有办法只能卸掉python重装. 安装Anaconda Anaconda指的 ...