LeetCode 1123. Lowest Common Ancestor of Deepest Leaves
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/
题目:
Given a rooted binary tree, return the lowest common ancestor of its deepest leaves.
Recall that:
- The node of a binary tree is a leaf if and only if it has no children
- The depth of the root of the tree is 0, and if the depth of a node is
d
, the depth of each of its children isd+1
. - The lowest common ancestor of a set
S
of nodes is the nodeA
with the largest depth such that every node in S is in the subtree with rootA
.
Example 1:
Input: root = [1,2,3]
Output: [1,2,3]
Explanation:
The deepest leaves are the nodes with values 2 and 3.
The lowest common ancestor of these leaves is the node with value 1.
The answer returned is a TreeNode object (not an array) with serialization "[1,2,3]".
Example 2:
Input: root = [1,2,3,4]
Output: [4]
Example 3:
Input: root = [1,2,3,4,5]
Output: [2,4,5]
Constraints:
- The given tree will have between 1 and 1000 nodes.
- Each node of the tree will have a distinct value between 1 and 1000.
题解:
For dfs, state needs current TreeNode root only. return value needs both depth and lca of deepest leaves node.
Divide and conquer, have left result and right result first. Then compare the depth, pick the bigger side, if both sides have same depth, return root with depth + 1.
Time Complexity: O(n). n is nodes count of the whole tree.
Space: O(logn).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lcaDeepestLeaves(TreeNode root) {
if(root == null){
return root;
} return lcaDfs(root).node;
} private Pair lcaDfs(TreeNode root){
if(root == null){
return new Pair(root, 0);
} Pair l = lcaDfs(root.left);
Pair r = lcaDfs(root.right);
if(l.depth > r.depth){
return new Pair(l.node, l.depth + 1);
}else if(l.depth < r.depth){
return new Pair(r.node, r.depth + 1);
}else{
return new Pair(root, l.depth + 1);
}
}
} class Pair{
TreeNode node;
int depth;
public Pair(TreeNode node, int depth){
this.node = node;
this.depth = depth;
}
}
LeetCode 1123. Lowest Common Ancestor of Deepest Leaves的更多相关文章
- [LeetCode] 1123. Lowest Common Ancestor of Deepest Leaves 最深叶结点的最小公共父节点
Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall that: Th ...
- 【leetcode】1123. Lowest Common Ancestor of Deepest Leaves
题目如下: Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall th ...
- 1123. Lowest Common Ancestor of Deepest Leaves
link to problem Description: Given a rooted binary tree, return the lowest common ancestor of its de ...
- Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)
Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves) 深度优先搜索的解题详细介绍,点击 ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the ...
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
随机推荐
- Python连载31-threading的使用
一. 例子:我们对传参是有要求的必须传入一个元组,否则报错 二. import _thread as thread import time def loop1(in1): print("St ...
- 基于canvas自动化运维工具
首先我们的工具绝对顶尖,绝对绚丽.如果有需要代码,可以加我微信索取.18500591275 前几天有个客户找到我,问我这个能不能做,我看自己也干了10年前端了,实在做不了,后来人家说给你10000你能 ...
- mybatis 的 dao 接口跟 xml 文件里面的 sql 是如何建立关系的?
mybatis 会先解析这些xml 文件,通过 xml 文件里面的命名空间 (namespace)跟dao 建立关系:然后 xml 中的每段 sql 会有一个id 跟 dao 中的接口进行关联. 那么 ...
- torch_02_多项式回归
""" torch.float64对应torch.DoubleTensor torch.float32对应torch.FloatTensor 将真实函数的数据点能够拟合成 ...
- 优先队列 + 模拟 - HDU 5437 Alisha’s Party
Alisha’s Party Problem's Link Mean: Alisha过生日,有k个朋友来参加聚会,由于空间有限,Alisha每次开门只能让p个人进来,而且带的礼物价值越高就越先进入. ...
- 关于预装操作系统的ThinkPad的分区建议
Think的个人电脑产品大部分预装有正版操作系统,当前新产品出厂时默认都是一个大分区“C”和一个恢复分区“Q”,很多用户都会要求客服人员提供分区服务,在这里我简单说一下关于分区的几点注意事项望各位参考 ...
- Entity Framework 学习系列(4) - EF 增删改
目录 写在前面 一.开发环境 二.创建项目 三.新增 1.单表新增 2.批量新增 3.多表新增 四.编辑 1.先查询,后编辑 2.创建实体,后编辑 五.删除 写在前面 在上一小节中,学习了如何 通过C ...
- vue中通过WeixinJSBridge关闭微信公众号当前页面,返回微信公众号首页
之前有个需求,点击菜单进入到微信公众号模块,然后点击返回的时候不知道到哪里去,后来觉得点返回的时候直接关闭页面,但是window.close()并不能关闭页面,然后经过查找资料,发现通过以下方法可以 ...
- spring AOP的两种配置
xml配置 定义要被代理的方法的接口 public interface TestAop { public void print(String s); } 实现上述接口 public class Tes ...
- Android 中发送邮件
第一步.导入第三方jar包 Android实现发送邮件,首先需要依赖additional.jar.mail.jar和activation.jar这3个jar包. Google提供了下载地址:https ...