link to problem

Description:

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.

Solution:

class Solution:
def lcaDeepestLeaves(self, root: TreeNode) -> TreeNode: def helper(node):
if not node:
return [node, 0]
if not node.left and not node.right:
return [node, 0] if not node.right:
left_node, left_dep = helper(node.left)
return [left_node, left_dep + 1] if not node.left:
right_node, right_dep = helper(node.right)
return [right_node, right_dep + 1] left_node, left_dep = helper(node.left)
right_node, right_dep = helper(node.right)
if left_dep > right_dep:
return [left_node, left_dep + 1]
elif left_dep < right_dep:
return [right_node, right_dep + 1]
else:
return [node, left_dep + 1] return helper(root)[0]

Notes:

DFS

recursion

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

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/ 题目: Given a rooted b ...

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

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

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

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

  5. LeetCode Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

  6. A1143. Lowest Common Ancestor

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  7. PAT A1143 Lowest Common Ancestor (30 分)——二叉搜索树,lca

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  8. 1143 Lowest Common Ancestor

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  9. PAT 甲级 1143 Lowest Common Ancestor

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 The lowest common ance ...

随机推荐

  1. Bugku-CTF加密篇之来自宇宙的信号(银河战队出击)

    来自宇宙的信号 银河战队出击 flag格式 flag{字母小写}    

  2. python爬虫-----Python访问http的几种方式

    爬取页面数据,我们需要访问页面,发送http请求,以下内容就是Python发送请求的几种简单方式: 会使用到的库  urllib   requests 1.urlopen import urllib. ...

  3. Vue2.0 中,“渐进式框架”和“自底向上增量开发的设计”这两个概念是什么?(转)

    https://www.zhihu.com/question/51907207?rf=55052497 徐飞 在我看来,渐进式代表的含义是:主张最少. 每个框架都不可避免会有自己的一些特点,从而会对使 ...

  4. Go包

    1. 导入包和init函数 init().main() 是 go 语言中的保留函数.我们可以在源码中,定义 init() 函数.此函数会在包被导入时执行,例如如果是在 main 中导入包,包中存在 i ...

  5. 在一个C程序中,main()函数可以放在哪?

    C语言规定,在一个C程序中,main()函数的位置(). A.必须在系统调用的库函数之后 B.必须在程序的开始 C.必须在程序的最后 D.可以在任意位置 答案:D [解析] 每个C程序有且只有一个主函 ...

  6. CSS学习(3)样式表

    如何插入样式表 插入样式表的方法有三种: 外部样式表(External style sheet) 内部样式表(Internal style sheet) 内联样式(Inline style) 外部样式 ...

  7. Mysql数据库内置功能之函数

    一 函数 MySQL中提供了许多内置函数,例如: 一.数学函数 ROUND(x,y) 返回参数x的四舍五入的有y位小数的值 RAND() 返回0到1内的随机值,可以通过提供一个参数(种子)使RAND( ...

  8. 【算法】dsu on tree初探

    dsu on tree的本质是树上的启发式合并,它利用启发式合并的思想,可以将O(N^2)的暴力优化成O(NlogN),用于不带修改的子树信息查询. 具体如何实现呢?对于一个节点,继承它重儿子的信息, ...

  9. git和github的关系以及简单易懂的理解

    git和github的关系   写在前面:我身边好多人问我git和github的区别,想必对于好多人没学过的大佬们恐怕也是一脸懵逼,但是不知道也是不行的,所以我今天就来讲一讲这二者的区别和联系. 用一 ...

  10. 第五节: 前后端交互之Promise用法和Fetch用法

    一. Promise相关 1.说明 主要解决异步深层嵌套的问题,promise 提供了简洁的API 使得异步操作更加容易 . 2.入门使用 我们使用new来构建一个Promise Promise的构造 ...