1123. Lowest Common Ancestor of Deepest Leaves
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 isd+1
. - The lowest common ancestor of a set
S
of nodes is the nodeA
with the largest depth such that every node in S is in the subtree with rootA
.
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的更多相关文章
- [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 ...
- LeetCode 1123. Lowest Common Ancestor of Deepest Leaves
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/ 题目: Given a rooted b ...
- 【leetcode】1123. Lowest Common Ancestor of Deepest Leaves
题目如下: Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall th ...
- Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)
Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves) 深度优先搜索的解题详细介绍,点击 ...
- LeetCode Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- 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 ...
- 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 ...
- 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 ...
- PAT 甲级 1143 Lowest Common Ancestor
https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 The lowest common ance ...
随机推荐
- jsoup学习待续
1.Jsoup简介 Jsoup是一个java html解析器.它是一个用于解析HTML文档的java库.Jsoup提供api来从URL或HTML文件中提取和操作数据.它使用DOM,CSS和类似 Jqu ...
- android:Android 6.0权限控制代码封装
新建的Activity类可以继承这个Activity,这个类封装了关于新版的权限处理相关的代码 使用方法: package com.glsite.phone; import android.conte ...
- Linux下调试caffe
参考博客:https://blog.csdn.net/xiaoyezi_1834/article/details/50724875 使用Anjuta 我使用的是ubuntu18.04,安装命令: su ...
- java获取配置文件中变量值
在resources 目录下新建config.properties文件 #文件保存路径 filePath=E:\\images\\file 工具类 public class ConfigUtil { ...
- mysql开启远程访问及相关权限控制
开启mysql远程访问: 授予用户user 密码 passwd 所有权限 所有主机IP可访问 授权语句:Grant <权限> on 表名[(列名)] to 用户 With grant op ...
- tomcat6w.exe启动tomcat
在使用tomcat中,我们可能经常点击startup.bat来启动tomcat,但也不少通过tomcat6w.exe来启动的. 但是当我们点击tomcat6w.exe的时候会报错,信息如下:提示 指定 ...
- Markdown编辑器软件安装及问题处理
一.Markdown简介 MarkdownPad是Windows下的一个多功能Markdown编辑器 Markdown是一门编辑语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式,可以用来对 ...
- 关于package.json和package-lock.json的区别
package.json文件记录你项目中所需要的所有模块.当你执行npm install的时候,node会先从package.json文件中读取所有dependencies信息,然后根据depende ...
- C语言程序设计(一)
目录: 1. 向屏幕输出一行文字 2. 输出两个函数的和 3. 函数一 4. 函数二 5. 求两个数的最大值 6. printf和scanf函数的返回值 7. 输入一个成绩,输出所对应的 ...
- GTA5整合包
链接:https://pan.baidu.com/s/1WUvLMyTcQYsw3wi6OfJfJA 提取码:jcpm