[LeetCode] 538 100

发布时间:2019-07-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了[LeetCode] 538 100脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

538. Convert BST to Greater Tree

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.

Input: The root of a Binary Search Tree like this:
              5
            /   
           2     13

Output: The root of a Greater Tree like this:
             18
            /   
          20     13

solution:

  1. 先更新父节点,再更新左子树,右子树的做法。慢!
class Solution:
    def convertBST(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        def tree_sum(root):
            if not root:
                return 0
            return root.val + tree_sum(root.left) + tree_sum(root.right)
        def f(node, accum):
            if not node:
                return None
            right_sum = tree_sum(node.right)
            node.val = node.val + accum + right_sum
            if node.left:
                f(node.left, node.val)
            if node.right:
                f(node.right, accum)
        f(root, 0)
        return root
  1. 递归地先计算右节点,父节点和左节点
class Solution:
    def convertBST(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        self.curr_sum = 0   # 用于记录累加的值
        return convert_help(root)
        
        def convert_help(root):
            if not root:
                return 
            convert_help(root.right)
            root.val = root.val + self.curr_sum
            self.curr_sum = root.val
            convert_help(root.left)
            return root

100. Same Tree

Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Input:     1         1
          /        / 
         2   3     2   3

        [1,2,3],   [1,2,3]

Output: true
Input:     1         1
          /           
         2             2

        [1,2],     [1,null,2]

Output: false
Input:     1         1
          /        / 
         2   1     1   2

        [1,2,1],   [1,1,2]

Output: false

solution:

class Solution:
    def isSameTree(self, p, q):
        """
        :type p: TreeNode
        :type q: TreeNode
        :rtype: bool
        """
        if not q and not p:
            return True
        elif q and not p:
            return False
        elif not q and p:
            return False
        elif q.val == p.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right):
            return True
        else:
            return False
class Solution:
    def isSameTree(self, p, q):
        """
        :type p: TreeNode
        :type q: TreeNode
        :rtype: bool
        """
        if p and q:
            return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
        return p is q

脚本宝典总结

以上是脚本宝典为你收集整理的[LeetCode] 538 100全部内容,希望文章能够帮你解决[LeetCode] 538 100所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: