zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    mac下安装oracle客户端
    Mac下安装tomcat
    eclipse上Git使用
    深拷贝1兼容多构造函数
    Oracle查询表大小,更新或查询太慢的时候
    Redis弱密码修改
    学习参考
    c#简单类型转换As<T>
    NPM相关
    java项目和npm项目命令窗口部署方便调试
  • 原文地址:https://www.cnblogs.com/xxxxxiaochuan/p/13246952.html
Copyright © 2011-2022 走看看