【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
Sof nodes is the nodeAwith 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) 深度优先搜索的解题详细介绍,点击 ...
随机推荐
- robotframework之常用系统关键字
常用系统关键字此处做些记录,后续根据实际应用陆续补充 1.变量声明 ${a} Set Variable hello 2.表单嵌套 Select Frame Xpath=//* [@] Unselect ...
- Unity3D 协程 Coroutine
协程(Coroutine)的概念存在于很多编程语言,例如Lua.ruby等.而由于Unity3D是单线程的,因此它同样实现了协程机制来实现一些类似于多线程的功能,但是要明确一点协程不是进程或线程,其执 ...
- Python学习之==>常用模块
一.string模块 import string print(string.ascii_letters) # 所有大小写字母 print(string.ascii_lowercase) # 所有小写字 ...
- IIS安全狗问题
1.没有安装以前网站运行正常,安装IIS全狗以后,ajaxpro2出现,找不到任何问题,卸载安全狗以后正常. 2.很久以前遇到的一个问题,有一款NET的cms系统,也是安装了安全狗以后不正常,忘记了c ...
- FTP搭建YUM源服务器
一.FTP搭建YUM源服务器 1.服务器 挂载centos镜像[root@localhost ~]#yum install vsftpd[root@localhost ~]#systemctl sta ...
- 浅谈html5在vr中的应用
使用过HTML5制作动画过程的开发者都知道,HTML5页面给人一种逼真的感觉,同时HTML也是可以制作VR页面,但是需要你熟练HTML5与JavaScript开发过程,所以在有必要的情况下,我们可以用 ...
- Vue-cli项目与element导航菜单控件的结合使用以及遇到的问题
1.基本使用 第一种常用写法:导航菜单与 router-view 的配合使用 将所用的导航菜单数据编写成一个数组的形式,提高维护性: 在utils工具文件夹中建立utils.js文件: import ...
- Express中间件body-parser
在http请求种,POST.PUT.PATCH三种请求方法中包含着请求体,也就是所谓的request,在Nodejs原生的http模块中,请求体是要基于流的方式来接受和解析. body-parser是 ...
- SCUT - G - 魔法项链 - 树状数组
https://scut.online/contest/30/G 很久以前做的一个东西,当时是对R排序之后树状数组暴力统计当前区间的前缀和.每有一个元素出现在R的范围内,就解除他的同样元素的影响,在他 ...
- P2220 [HAOI2012]容易题
传送门 首先 $(\sum_{i=1}^{n}a_i)(\sum_{i=1}^{m}b_i)$ 展开以后包含了所有 $ab$ 两两相乘的情况并且每种组合只出现一次 发现展开后刚好和题目对序列价值的定义 ...