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. next.config.js

    const configs = { // 编译文件的输出目录 distDir: 'dest', // 是否给每个路由生成Etag generateEtags: true, // 页面内容缓存配置 on ...

  2. c++中sort函数调用报错Expression : invalid operator <的内部原理

    当我们调用sort函数进行排序时,中的比较函数如果写成如下 bool cmp(const int &a, const int &b) { if(a!=b) return a<b; ...

  3. 003 CSS汇总

    字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...

  4. OBS_Classic经典版框架

    一,简介 OBS(open boardcast server),是一个用于直播的开源软件. 官方网站:https://obsproject.com/ 代码托管地址:https://github.com ...

  5. 服务器部署网站后,公网ip可以访问,域名不能访问问题(稳)

    出现问题 这几天我网站已经部署到vps上,域名也备好案,想使用域名指向我们公网ip.指完发现用域名访问不了网站,但是公网ip可以.于是看了网上资料,好像是要清除浏览器DNS缓存,我清完没用.然后发现我 ...

  6. 包、logging模块、hashlib模块、openpyxl模块、深浅拷贝

    包.logging模块.hashlib模块.openpyxl模块.深浅拷贝 一.包 1.模块与包 模块的三种来源: 1.内置的 2.第三方的 3.自定义的 模块的四种表现形式: 1.py文件 2.共享 ...

  7. wcftestclient test soap API

    soap 类API测试方法: 1. 打开developer command prompt, 输入:wcftestclient 2. 选中“My Service Project”, 再Add servi ...

  8. 【C语言】(数组方式)求n名同学的平均成绩

    原理就不说了 代码: #include <stdio.h> int main() { ], sum = ; int i; printf("请输入5名童鞋的成绩:\n") ...

  9. 写作环境搭建(git+github+markdown+jekyll)

    转载自: https://site.douban.com/196781/widget/notes/12161495/note/264946576/ 2013-03-04 19:33:10   --- ...

  10. linux 开启普通用户sudo root权限操作获取免密

    root 身份登陆 $ visudo然后进入修改配置找到 root    ALL=(ALL) ALL 在下面增加: yourusername ALL=(ALL)   NOPASSWD:  ALL ex ...