题目如下:

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.

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的更多相关文章

  1. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. [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 ...

  3. LeetCode 1123. Lowest Common Ancestor of Deepest Leaves

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/ 题目: Given a rooted b ...

  4. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...

  5. 【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 ...

  6. 【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 ...

  7. 【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 ...

  8. 1123. Lowest Common Ancestor of Deepest Leaves

    link to problem Description: Given a rooted binary tree, return the lowest common ancestor of its de ...

  9. Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)

    Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves) 深度优先搜索的解题详细介绍,点击 ...

随机推荐

  1. JVM监控工具之JVisualVM

    一.简介 JVisualVM是Netbeans的profile子项目,已在JDK6.0 update 7 中自带(bin/jvisualvm.exe),能够监控线程,内存情况,查看方法的CPU时间和内 ...

  2. 几家大的券商的PB系统以及算法交易概况大致是怎样的?

    PB的定位是托管-清算-交易.目前的PB系统方面的竞争点主要放在了交易环节(毕竟托管和清算没有多大的差异).目前的pb交易环节的技术提供有恒生.讯投.金证.同花顺等,以满足私募及高净值个人多样化交易和 ...

  3. powered by Fernflower decompiler

    About Fernflower Fernflower is the first actually working analytical decompiler for Java and probabl ...

  4. CentOS 7.3下使用yum安装MySQL

    CentOS 7的yum源中默认是没有mysql的,要先下载mysql的repo源. 1.下载mysql的repo源 $ wget http://repo.mysql.com/mysql-commun ...

  5. Scratch少儿编程系列:(四)脚本选项卡说明

    脚本选项卡下面主要包括:运动.事件.外观.控制.声音.侦测.画笔.运算.数据.更多积木. 一.运动:所谓运动,指的是角色的“运动”.如移动10步,右转15度,左转15度等. [注] 将“脚本”从选项卡 ...

  6. 递归算法之不用乘号的乘法——用位移实现乘法(dart语言实现)

    前两天突发奇想,写一个乘法的实现,但不用乘号*.并测试一下性能如何.因此就有了下面的代码:(本文主要目的是为了玩递归和位移,因此仅限自然数) 首先,标准乘法: int commonMultiplica ...

  7. Fedora添加软件桌面快捷方式

    以下以添加Eclipse为例 在桌面上新建Eclipse.desktop 文件,向其写入如下代码 [Desktop Entry] Name=Eclipse Comment=用Eclipse开发 Exe ...

  8. P3158 [CQOI2011]放棋子(dp+组合数)

    P3158 [CQOI2011]放棋子 放棋子的顺序和方案数无关,所以可以从按颜色递推 设$f[u][p][k]$为放到第$u$种颜色,所剩空间$p*k$的方案数 $g[u][i][j]$表示第$u$ ...

  9. TMS320F28335——IO控制/定时计操作

    一.实现GPIO控制 1.硬件连接 从电路原理图上看来,LED灯是接在GPIO34 上的. 2.IO设置 2.1设置功能 GPXMUX1/2:功能选择寄存器 GPXMUX1/2    每组 IO 一般 ...

  10. activemq高可用

    这里是基于 zookeeper 选举方式实现的集群配置,服务器过半数才可提供服务,所以是2n+1台这里以三台为例. 只有master节点能提供服务,slave节点无法提供服务,只有当master节点挂 ...