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 ...
随机推荐
- 矩阵-bzoj1898
This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. BZOJ1898 ...
- 关于mybatis中多参数传值
如果前台单独传递多个参数,并且未进行封装,在mybatis的sql映射文件中需要以下标的形式表示传递的参数,从0开始 即:where name=#{0} and password =#{1}:0表示第 ...
- 《gPRC使用protobuf构建微服务》阅读笔记
首先我需要去了解一些概念,根据百度百科了解到: l 微服务架构:微服务架构是一项在云中部署应用和服务的新技术.微服务可以在“自己的程序”中运行,并通过“轻量级设备与HTTP型API进行沟通”. l ...
- linux下删除文件夹
---恢复内容开始--- 4月份左右接触linux,一直到现在,收获不多,原因是因为我没有足够的努力,其实这段时间以来我也很自责. 今天学习linux进程调度等知识,使用小红帽时,准备删除一个无用的文 ...
- C语言传递二维数组
方法一, 形参给出第二维的长度. 例如: #include <stdio.h> ] ) { int i; ; i < n; i++) printf("/nstr[%d] = ...
- 【C语言】创建一个函数,并调用比较两个数的大小
#include <stdio.h> int max(int x,int y) { if(x>=y) return x; else return y; } main() { int ...
- 通过POI实现上传EXCEL的批量读取数据写入数据库
最近公司新增功能要求导入excel,并读取其中数据批量写入数据库.于是就开始了这个事情,之前的文章,记录了上传文件,本篇记录如何通过POI读取excel数据并封装为对象上传. 上代码: 1.首先这是一 ...
- 关于this和base的区别
一句话总结:在有冲突得时候,base和this能够进行区分,在没有冲突得时候,是一样得. 基于成员调用 基于构造方法 参考: http://www.cnblogs.com/reommmm/archiv ...
- Tensorflow2.0默认下载数据集到C盘的修改方法
jupyter(Win版本)下载数据集会默认到C盘下,Linux会默认到root下,修改方式如下· tf1.x: import os import tensorflow as tftf.disable ...
- 调用webservice服务(通过反射的方式动态调用)
调用 ";//系统类别 var jkxlh = "";//接口序列号 var jkid = "68W05";//接口id string WriteXm ...