美文网首页
LeetCode #124 #236 #235 2018-08-

LeetCode #124 #236 #235 2018-08-

作者: 40巨盗 | 来源:发表于2018-08-11 07:24 被阅读0次

    124. Binary Tree Maximum Path Sum

    https://leetcode.com/problems/binary-tree-maximum-path-sum/description/

    这道题是求树的路径和的题目,不过和平常不同的是这里的路径不仅可以从根到某一个结点,而且路径可以从左子树某一个结点,然后到达右子树的结点,就像题目中所说的可以起始和终结于任何结点。在这里树没有被看成有向图,而是被当成无向图来寻找路径。因为这个路径的灵活性,我们需要对递归返回值进行一些调整,而不是通常的返回要求的结果。在这里,函数的返回值定义为以自己为根的一条从根到子结点的最长路径(这里路径就不是当成无向图了,必须往单方向走)。这个返回值是为了提供给它的父结点计算自身的最长路径用,而结点自身的最长路径(也就是可以从左到右那种)则只需计算然后更新即可。这样一来,一个结点自身的最长路径就是它的左子树返回值(如果大于0的话),加上右子树的返回值(如果大于0的话),再加上自己的值。而返回值则是自己的值加上左子树返回值或者右子树返回值或者0(注意这里是“或者”,而不是“加上”,因为返回值只取一支的路径和)。所以整个算法就是维护这两个量,一个是自己加上左或者右子树最大路径作为它的父节点考虑的中间量,另一个就是自己加上左再加上右作为自己最大路径。在过程中求得当前最长路径时比较一下是不是目前最长的,如果是则更新。算法的本质还是一次树的遍历,所以时间复杂度是O(n),而空间上仍然是栈大小O(logn)
    代码如下:

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def maxPathSum(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            import sys
            self.max = -sys.maxsize
            self.helper(root)
            return self.max
        
        def helper(self, root):
            if not root:
                return 0
            left = self.helper(root.left)
            right = self.helper(root.right)
            value = max(left, 0) + max(right, 0) + root.val
            self.max = max(self.max, value)
            return max(0, left, right) + root.val
    

    236. Lowest Common Ancestor of a Binary Tree

    https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/

    从下向上Conquer,当遇到第一个包含2个所给结点的父结点时,返回该结点即可。
    代码如下:

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def lowestCommonAncestor(self, root, p, q):
            """
            :type root: TreeNode
            :type p: TreeNode
            :type q: TreeNode
            :rtype: TreeNode
            """
            if not root or root == p or root == q:
                return root
            left = self.lowestCommonAncestor(root.left, p, q)
            right = self.lowestCommonAncestor(root.right, p, q)
            if left and right:
                return root
            if left:
                return left
            if right:
                return right
            return None
    

    235. Lowest Common Ancestor of a Binary Search Tree

    https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/

    Just walk down from the whole tree's root as long as both p and q are in the same subtree (meaning their values are both smaller or both larger than root's). This walks straight from the root to the LCA, not looking at the rest of the tree, so it's pretty much as fast as it gets.
    代码如下:

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def lowestCommonAncestor(self, root, p, q):
            """
            :type root: TreeNode
            :type p: TreeNode
            :type q: TreeNode
            :rtype: TreeNode
            """
            if p.val < root.val > q.val:
                return self.lowestCommonAncestor(root.left, p, q)
            if p.val > root.val < q.val:
                return self.lowestCommonAncestor(root.right, p, q)
            return root
    

    相关文章

      网友评论

          本文标题:LeetCode #124 #236 #235 2018-08-

          本文链接:https://www.haomeiwen.com/subject/fbrfbftx.html