原题链接在这里: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. PHP获取cookie、Token、模拟登录、抓取数据、解析生成json

    本文介绍使用PHP获取cookie,获取Token.以及模拟登录.然后抓取数据.最后解析生成json的的过程. 0. 设置Cookie路径 set_time_limit(0); //使用的cookie ...

  2. Pandas中查看列中数据的种类及个数

    Pandas中查看列中数据的种类及个数 读取数据 import pandas as pd import numpy as np filepath = 'your_file_path.csv' data ...

  3. axios源码入口以及公用方法

    axios学习笔记(公用方法) 源码地址 找到入口文件 axios/lib/axios.js var utils = require('./utils'); var bind = require('. ...

  4. Hbase Filter之FilterList

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl/p/7098138.html 转载请注明出处 我们知道Hbase的Scan经常需要用到filter来过滤表中的数 ...

  5. 『Norma 分治』

    Norma Description Input Format 第1行,一个整数N: 第2~n+1行,每行一个整数表示序列a. Output Format 输出答案对10^9取模后的结果. Sample ...

  6. Nginx-Tomcat 等运维常用服务的日志分割-logrotate

    目录 一 .Nginx-Tomcat 等常用服务日志分析 Nginx 日志 Tomcat日志 MongoDB 日志 Redis 日志 二 .日志切割服务 logrotate 三.日志切割示例 Ngin ...

  7. 【JZOJ】2126. 最大约数和

    题目大意 选取和不超过S的若干个不同的正整数,使得所有数的约数(不含它本身)之和最大. 分析 把我们分解出来的因数进行合并,存在一个不知名的数组里,然后我们大可开始我们的迪屁!!(bag),我们可以 ...

  8. linux教程:[3]配置Zookeeper开机启动

    ZooKeeper是Hadoop的正式子项目: Hadoop是一个分布式系统基础架构,由Apache基金会所开发: Zookeeper能够用来leader选举:也就是你有N+1台同样的服务器的时候又z ...

  9. 开发技术--pandas模块

    开发|pandas模块 整了一篇关于pandas模块的使用文章,方便检查自己的学习质量.自从使用了pandas之后,真的是被它的功能所震撼~~~ 前言 目前所有的文章思想格式都是:知识+情感. 知识: ...

  10. 学习笔记之UML ( Unified Modeling Language )

    Unified Modeling Language - Wikipedia https://en.wikipedia.org/wiki/Unified_Modeling_Language The Un ...