【leetcode】1123. Lowest Common Ancestor of Deepest Leaves
题目如下:
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
.Example 1:
Input: root = [1,2,3]
Output: [1,2,3]
Explanation:
The deepest leaves are the nodes with values 2 and 3.
The lowest common ancestor of these leaves is the node with value 1.
The answer returned is a TreeNode object (not an array) with serialization "[1,2,3]".Example 2:
Input: root = [1,2,3,4]
Output: [4]Example 3:
Input: root = [1,2,3,4,5]
Output: [2,4,5]Constraints:
- The given tree will have between 1 and 1000 nodes.
- Each node of the tree will have a distinct value between 1 and 1000.
解题思路:首先求出最深的所有叶子节点的索引,存入列表中;然后把索引列表排序,取出最大的索引值记为val,显然其父节点的索引值是(val - val%2)/2,把父节点的索引值加入索引列表,并重复这个过程,知道索引列表中所有的值都相等为止,这个值就是所有最深叶子的节点的公共父节点。得到公共父节点索引值,可以反推出从根节点到该节点的遍历路径,从而可以求出具体的节点。
代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
dic = {}
maxLevel = 0
def recursive(self,node,level,number):
self.maxLevel = max(self.maxLevel,level)
self.dic[level] = self.dic.setdefault(level,[]) + [number]
if node.left != None:
self.recursive(node.left,level+1,number*2)
if node.right != None:
self.recursive(node.right,level+1,number*2+1)
def lcaDeepestLeaves(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
self.dic = {}
self.maxLevel = 0
self.recursive(root,1,1)
node_list = sorted(self.dic[self.maxLevel])
def isEqual(node_list):
v = node_list[0]
for i in node_list:
if v != i:
return False
return True
while not isEqual(node_list):
node_list.sort(reverse=True)
val = node_list.pop(0)
val = (val - val%2)/2
node_list.append(val)
number = node_list[0]
path = []
while number > 1:
if number%2 == 0:
path = ['L'] + path
else:
path = ['R'] + path
number = (number - number % 2) / 2 node = root
while len(path) > 0:
p = path.pop(0)
if p == 'L':node = node.left
else:node = node.right
return node
【leetcode】1123. Lowest Common Ancestor of Deepest Leaves的更多相关文章
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree
题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in th ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)
Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest com ...
- 1123. Lowest Common Ancestor of Deepest Leaves
link to problem Description: Given a rooted binary tree, return the lowest common ancestor of its de ...
- Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)
Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves) 深度优先搜索的解题详细介绍,点击 ...
随机推荐
- Python学习之==>操作Redis
一.redis简介 1.非关系型数据库 2.数据全部是存在内存里面 3.性能非常好,每秒支持30w次读写 4.可以通过备份数据库,把数据存到磁盘上来实现数据的持久化 二.操作redis 1.strin ...
- 20191110 Spring Boot官方文档学习(4.1)
4. Spring Boot功能 4.1.Spring应用 便捷的启动方式: public static void main(String[] args) { SpringApplication.ru ...
- xmake v2.2.9 发布, 新增c++20 modules的实验性支持
这个版本没啥太大新特性,主要对c++20 modules进行了实验性支持,目前支持clang/msvc编译器,除此之外改进了不少使用体验,并且提高了一些稳定性. 另外,这个版本新增了socket.io ...
- Hive-生成一个大文件(小文件合并)
set hive.execution.engine=mr; --在 map-reduce 作业结束时合并小文件.如启用,将创建 map-only 作业以合并目标表/分区中的文件. set hive.m ...
- SQL Server中的扩展事件学习系列
SQL Server 扩展事件(Extented Events)从入门到进阶(1)——从SQL Trace到Extented Events SQL Server 扩展事件(Extented Event ...
- 转:mysql datetime类型精确到毫秒、微秒的问题
原文地址:mysql datetime类型精确到毫秒.微秒的问题 mysql里面的datetime类型的精确度是可以到1/ 10 ^ 6 秒的某些客户端(如navicat for mysql)的显示经 ...
- Bootstrap字体图标Font-Awesome 使用教程
转载自:https://blog.csdn.net/crper/article/details/46293295 以备记录使用.
- 不能将X*类型的值分配到X*类型的实体问题的解决方法
今天在学习链表的过程中遇到了这个问题,我用如下方法定义了一个结构体,然后这个函数想要在链表头插入一个节点.但是在函数的最后一行却出现了报错:不能将MyLinkedList * 类型的值分配到MyLin ...
- jquery中的插件EChars的使用
首先,进入EChars的官网下载页面:http://echarts.baidu.com/download.html 下载自己需要的版本. 引入jquery包和echars,进入官网的实例:htt ...
- thinkphp5 隐藏前台入口文件index.php 后台入口文件admin.php不隐藏
情景:应用目录下有两个模块 admin(后台) 和 home(前台) 需求:1.访问前台(home)时隐藏index.php 即 域名/home/前台控制器/前台控制器里的方法 这样的访问模式 2. ...