• 边工作边刷题:70天一遍leetcode: day 60


    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
    • 最终可行的是用map存结点count,而不是在stack里,类似Balanced Binary Tree
    # 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
                
                
    
    
  • 相关阅读:
    Java IO<2> 输入/输出流 FileInputStream/FileOutputStream
    springcloud1
    Java IO<4>Java io与装饰器模式
    springcloud2
    操作系统——第四章课后习题答案01
    操作系统学习笔记_04
    不夸张地说,在程序员里面,我算得上玩EXCEL比较溜的!
    IIS 发布 Vue2 + Net Core Api
    算法
    《无垠的太空(9).利维坦陨落》插曲:梦中人(2)
  • 原文地址:https://www.cnblogs.com/absolute/p/5690329.html
Copyright © 2020-2023  润新知