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:

  1. class Solution:
  2. def lcaDeepestLeaves(self, root: TreeNode) -> TreeNode:
  3.  
  4. def helper(node):
  5. if not node:
  6. return [node, 0]
  7. if not node.left and not node.right:
  8. return [node, 0]
  9.  
  10. if not node.right:
  11. left_node, left_dep = helper(node.left)
  12. return [left_node, left_dep + 1]
  13.  
  14. if not node.left:
  15. right_node, right_dep = helper(node.right)
  16. return [right_node, right_dep + 1]
  17.  
  18. left_node, left_dep = helper(node.left)
  19. right_node, right_dep = helper(node.right)
  20. if left_dep > right_dep:
  21. return [left_node, left_dep + 1]
  22. elif left_dep < right_dep:
  23. return [right_node, right_dep + 1]
  24. else:
  25. return [node, left_dep + 1]
  26.  
  27. 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. jsoup学习待续

    1.Jsoup简介 Jsoup是一个java html解析器.它是一个用于解析HTML文档的java库.Jsoup提供api来从URL或HTML文件中提取和操作数据.它使用DOM,CSS和类似 Jqu ...

  2. android:Android 6.0权限控制代码封装

    新建的Activity类可以继承这个Activity,这个类封装了关于新版的权限处理相关的代码 使用方法: package com.glsite.phone; import android.conte ...

  3. Linux下调试caffe

    参考博客:https://blog.csdn.net/xiaoyezi_1834/article/details/50724875 使用Anjuta 我使用的是ubuntu18.04,安装命令: su ...

  4. java获取配置文件中变量值

    在resources 目录下新建config.properties文件 #文件保存路径 filePath=E:\\images\\file 工具类 public class ConfigUtil { ...

  5. mysql开启远程访问及相关权限控制

    开启mysql远程访问: 授予用户user 密码 passwd 所有权限 所有主机IP可访问 授权语句:Grant <权限> on 表名[(列名)] to 用户 With grant op ...

  6. tomcat6w.exe启动tomcat

    在使用tomcat中,我们可能经常点击startup.bat来启动tomcat,但也不少通过tomcat6w.exe来启动的. 但是当我们点击tomcat6w.exe的时候会报错,信息如下:提示 指定 ...

  7. Markdown编辑器软件安装及问题处理

    一.Markdown简介 MarkdownPad是Windows下的一个多功能Markdown编辑器 Markdown是一门编辑语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式,可以用来对 ...

  8. 关于package.json和package-lock.json的区别

    package.json文件记录你项目中所需要的所有模块.当你执行npm install的时候,node会先从package.json文件中读取所有dependencies信息,然后根据depende ...

  9. C语言程序设计(一)

    目录: 1.  向屏幕输出一行文字 2.  输出两个函数的和 3.  函数一 4.  函数二 5.  求两个数的最大值 6.  printf和scanf函数的返回值 7.  输入一个成绩,输出所对应的 ...

  10. GTA5整合包

    链接:https://pan.baidu.com/s/1WUvLMyTcQYsw3wi6OfJfJA 提取码:jcpm