题目如下:

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. TCP 首部格式

    <图解TCP/IP> 6.7  TCP的首部格式 TCP中没有表示包长度和数据长度的字段.可由IP层获知TCP的包长由TCP的包长可知数据的长度. 源端口号:表示发送端端口号,字段长16位 ...

  2. nginx详解反向代理,负载均衡,LNMP架构上线动态网站

    1.nginx介绍 nginx.org Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/ ...

  3. WinFrom控件双向绑定

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. unittest单元测试1

    一个简单的单元测试例子#coding:utf-8from selenium import webdriverimport unittestclass Baidu(unittest.TestCase): ...

  5. ARP协议基础

    ARP 什么是ARP协议 ARP协议是能够根据IP地址解析出该IP地址所在设备的MAC地址,叫(Address Resolution Protocol)地址解析协议 ARP地址的工作流程 当一台主机A ...

  6. IDEA工具与第三方工具集成

    IDEA工具与第三方工具集成 Tomcat部署 (一)配置Tomcat ->->->配置信息 常见问题: [1]注意部署异常:java.lang.OutOfMemoryError: ...

  7. 使用certbot 为nginx或openresty启用https

    certbot 使用letsencrypt 生成免费https证书 以下内容网上整理,原文地址 https://segmentfault.com/a/1190000005797776 -------- ...

  8. Docker数据持久化及实战(Nginx+Spring Boot项目+MySQL)

    Docker数据持久化: Volume: (1)创建mysql数据库的container docker run -d --name mysql01 -e MYSQL_ROOT_PASSWORD= my ...

  9. merge into 导致序列跳号

    For each row merged by a MERGE statement. The reference to NEXTVAL can appear in the merge_insert_cl ...

  10. JAVA基础--JAVA 集合框架(泛型、file类)

    一.集合总结 集合:Collection体系.Map体系. Collection体系:单列集合的共性操作规则. List:列表,可以重复,有下标,拥有特有的迭代器ListIterator. Array ...