原题链接在这里: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 is d+1.
  • The lowest common ancestor of a set S of nodes is the node A with the largest depth such that every node in S is in the subtree with root A.

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的更多相关文章

  1. [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 ...

  2. 【leetcode】1123. Lowest Common Ancestor of Deepest Leaves

    题目如下: Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall th ...

  3. 1123. Lowest Common Ancestor of Deepest Leaves

    link to problem Description: Given a rooted binary tree, return the lowest common ancestor of its de ...

  4. Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)

    Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves) 深度优先搜索的解题详细介绍,点击 ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. windows php 下的各个版本的资源下载地址

    历史版本: https://windows.php.net/downloads/releases/archives/ 新版本: https://windows.php.net/download/ 找的 ...

  2. tmp/ccdLyHub.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status

    其实就是一个问题,gcc只能编译.c文件,你如果取名为.cpp,那么gcc编译就会就会出现这个错误. 这种情况下: 1.用g++编译(.c 或.c++都可以编译) 2.仍用gcc编译,但是文件后缀改为 ...

  3. Laravel服务容器的绑定与解析

    本篇文章给大家带来的内容是关于Laravel服务容器的绑定与解析,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 前言   老实说,第一次老大让我看laravel框架手册的那天早上,我 ...

  4. 通过IP获取MAC地址例子(应用层)

    博客地址:http://home.cnblogs.com/u/zengjianrong/ 由于某种需求,需要获取某个ip的mac地址,在应用层实现例子如下代码. 流程:1. 先遍历arp表,若存在对应 ...

  5. Vue.js 源码分析(十四) 基础篇 组件 自定义事件详解

    我们在开发组件时有时需要和父组件沟通,此时可以用自定义事件来实现 组件的事件分为自定义事件和原生事件,前者用于子组件给父组件发送消息的,后者用于在组件的根元素上直接监听一个原生事件,区别就是绑定原生事 ...

  6. zipkin微服务调用链分析(python)

    一,概述 zipkin的作用 在微服务架构下,一个http请求从发出到响应,中间可能经过了N多服务的调用,或者N多逻辑操作,如何监控某个服务,或者某个逻辑操作的执行情况,对分析耗时操作,性能瓶颈具有很 ...

  7. golang学习笔记---命令源码文件接收参数(flag包)

    命令源码文件怎样接收参数 go标准库中有一个代码包专门用于接收和解析命令参数.这个包叫flag 实例1: package main import ( "flag" "fm ...

  8. ASP.NET Core 中的 Razor 文件编译

    asp .net core mvc 3.0 在编译的时候做了一些改变,有些view视图更改需要重新编译,你也可以配置运行时编译,不用每次更改都去重新生成,具体代码如下,从官方文档看到,做个记录. Ra ...

  9. Quartz.Net 删除一个Job

    Quartz.Net 删除Job 来博客园的第一篇文章先写个简单的,希望能帮助到大家. 步入正题: Quartz.Net有三个重要的概念,分别是 Scheduler .Job .Trigger.  S ...

  10. iTextSharp生成pdf含模板(二)---C#代码部分

    参考地址:https://www.cnblogs.com/ibeisha/p/itextsharp-pdf.html 一.先在程序中使用Nuget安装iTextSharp(我是创建的控制台程序) 二. ...