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 ...
随机推荐
- Java的内存需要划分成为5个部分:
Java的内存需要划分成为5个部分: 1.栈(Stack):存放的都是方法中的局部变量.方法的运行一定要在栈当中运行. 局部变量:方法的参数,或者是方法{}内部的变量 作用域:一旦超出作用域,立从栈内 ...
- 整理:WPF中CommandBindings的用法
原文:整理:WPF中CommandBindings的用法 目的:了解一下CommandBindings.InputBindings.ICommandSource中在WPF中扮演什么样的角色 Comma ...
- 一文搞懂 Flink 网络流控与反压机制
https://www.jianshu.com/p/2779e73abcb8 看完本文,你能get到以下知识 Flink 流处理为什么需要网络流控? Flink V1.5 版之前网络流控介绍 Flin ...
- web技术栈开发原生应用-多端共用一套代码
weex: vuejs开发原生应用 nativescript: vuejs开发原生应用 ReactNative = reactjs开发原生应用 ionic = angularjs 开发原生应用
- rsync 排除指定目录
背景 将Server1上的数据同步到Server2: Server1目录结构: /us_data/yahoo └── qlib ├── calendars ├── dataset_cache ├── ...
- 在HTML网页中嵌入脚本的方式
在HTML标记的事件属性中直接添加脚本 <!doctype html> <html> <head> <meta charset="utf-8&quo ...
- MTSC2019-深圳站 议题征集
议题截止时间 11月初 议题投递地址 topic@testerhome.com 臣一路走来,没有敌人,看见的都是朋友和师长 —司马懿 关于中国移动互联网测试大会 MTSC 大会(中国移动互联网测试 ...
- 教你玩转Git-合并冲突
Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件.Git 与 ...
- spark任务分配----TaskSchedulerImpl源码解析
TaskSchedulerImpl 上一篇讲到DAGScheduler根据shuffle依赖对作业的整个计算链划分成多个stage之后,就开始提交最后一个ResultStage,而由于stage之间的 ...
- 关于ionic2在IOS上点击延迟的问题
正常的点击事件, 不知道 为什么 ,在IOS上明显会延迟几百毫秒.. 加上tappable属性就可以解决了 <div tappable (click)="doClick()" ...