• 236. 二叉树的最近公共祖先-中序遍历-中等难度


    问题描述

    给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。

    百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

    例如,给定如下二叉树:  root = [3,5,1,6,2,0,8,null,null,7,4]

    示例 1:

    输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
    输出: 3
    解释: 节点 5 和节点 1 的最近公共祖先是节点 3。
    示例 2:

    输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
    输出: 5
    解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。
     

    说明:

    所有节点的值都是唯一的。
    p、q 为不同节点且均存在于给定的二叉树中。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree

    解答

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    '''
    
    中序遍历,然后遇到p时,把当前所走的路径添加到一个list1里面;遇到q时,把当前所走的路径添加到list2里面。
    最后得到两条路径,找出最深层的公共祖先即可。
    
    '''
    class Solution(object):
        def lowestCommonAncestor(self, root, p, q):
            if root == None:
                return
            route1 = []
            route2 = []
            def dfs(root, p, q, temp_route):
                if root.left == None and root.right == None:
                    temp_route.append(root)
                    if root == p and not route1:
                        route1.append([])
                        route1[-1].extend(temp_route)
                    elif root == q and not route2:
                        route2.append([])
                        route2[-1].extend(temp_route)
                    temp_route.pop()
                    return
                if root.left != None:
                    temp_route.append(root)
                    if root == p and not route1:
                        route1.append([])
                        route1[-1].extend(temp_route)
                    elif root == q and not route2:
                        route2.append([])
                        route2[-1].extend(temp_route)
                    dfs(root.left, p, q, temp_route)
                    temp_route.pop()
                if root.right != None:
                    temp_route.append(root)
                    if root == p and not route1:
                        route1.append([])
                        route1[-1].extend(temp_route)
                    elif root == q and not route2:
                        route2.append([])
                        route2[-1].extend(temp_route)
                    dfs(root.right, p, q, temp_route)
                    temp_route.pop()
            dfs(root, p, q, [])
            if route1 and route2:
                route1[0].reverse()
                route2[0].reverse()
                for i in route1[0]:
                    for j in route2[0]:
                        if i == j:
                            return i
            elif not route1 and route2:
                return q
            elif not route2 and route1:
                return p
            return

  • 相关阅读:
    关于json解析和所需jar
    Solr初步使用
    Appium-desktop使用时的一些配置
    mac电脑安装和配置tomcat步骤
    maven-reportng插件依赖添加
    maven-surefire插件配置
    mac电脑的一些操作
    元素的多种延时等待(&页面的超时处理)
    Mac系统搭建java+selenium+testng环境
    爬虫简介与request模块
  • 原文地址:https://www.cnblogs.com/xxxxxiaochuan/p/13246952.html
Copyright © 2020-2023  润新知