zoukankan      html  css  js  c++  java
  • 边工作边刷题:70天一遍leetcode: day 90

    Lowest Common Ancestor of a Binary Tree

    要点:这题和Binary Search Tree的版本递归都很简单,问题是如何用iteration来做。BST比较简单,直接沿一条分支搜索即可。而Binary Tree需要用post-order traversal的方法,开始想的时候试了几条路都不对,最终还是想出来了。以下为思考过程:

    • 开始想maintain一个global的left和right来标记左右子树中,但用global是不对的,left和right都是局部于子树的。

    • 并且如果向下展开时候遇到p或者q,可不可以提早回溯呢?答案显然是不能,因为另一个还没找到,要继续遍历

    • 然后想把左右存在stack中,其实没必要存2个,只需要存子树中结点出现的count的就可以

    • 但是stack是不行的,因为在访问后就pop了,无法根据left,right的值得到当前结点的count

    • global left/right variable行吗?也不行,因为left遍历完了,遍历right的时候要更新left,没法keep old left

    • 最终可行的是用map存结点count,而不是在stack里,类似Balanced Binary Tree

    • 为什么递归版本如果遇到p或者q可以提前返回?因为假设了结点一定存在,所以即使q在p的子树里,最终dfs返回的是p

    • 扩展1:print path: 要点就是==p or q不提前返回,继续找(只有rootNone直接返回): https://repl.it/EN14

    • 扩展2:如果p/q相同怎么办?不影响,遇到谁就返回谁

    • 扩展3:如果p/q不一定存在呢?影响:要用print path的递归方法,并且要加一个count判定是不是曾经遇到过2个。这样在返回p或q的情况下可以得到正确结果

    # 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):
        def lowestCommonAncestor(self, root, p, q):
            """
            :type root: TreeNode
            :type p: TreeNode
            :type q: TreeNode
            :rtype: TreeNode
            """
            if not root: return None 
            stk = [root]
            hmap = {}
            pre = None
            while stk:
                cur = stk[-1]
                if pre==None or pre.left==cur or pre.right==cur:
                    if cur.left:
                        stk.append(cur.left)
                    elif cur.right:
                        stk.append(cur.right)
                elif cur.left==pre:
                    if cur.right:
                        stk.append(cur.right)
                else:
                    count = 0
                    if cur.left:
                        count+=hmap[cur.left]
                    if cur.right:
                        count+=hmap[cur.right]
                    if cur==p or cur==q:
                        count+=1
                    if count==2:
                        return cur
                    hmap[cur]=count
                    stk.pop()
                
                pre = cur
            
            return None
                
    
  • 相关阅读:
    Sunday算法
    砝码称重 洛谷 1441
    树秀于林风必摧之——线段树
    常用stl(c++)
    Vue 根组件,局部,全局组件 | 组件间通信,案例组件化
    Win下JDK的安装和简单使用教程
    ubuntu服务器远程连接xshell,putty,xftp的简单使用教程
    ubuntu下安装pdo和pdo_mysql扩展
    服务器和域名的简单个人认知
    对大一一年的总结和对大二的规划
  • 原文地址:https://www.cnblogs.com/absolute/p/6041356.html
Copyright © 2011-2022 走看看