1. 程式人生 > >Python實現"左葉子之和"的兩種方法

Python實現"左葉子之和"的兩種方法

給定一顆二叉樹,返回它所有左葉子節點之和

Example:

    3
   / \
  9  20
    /  \
   15   7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

1:遞迴方法

def sumOfLeftLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        if not root.left and not root.right:   #當前節點不存在左右子樹
            return 0
        if not root.left and root.right:       #當前節點只有右子樹
            return self.sumOfLeftLeaves(root.right)
        if root.left and not root.right:     #當前節點只有左子樹
            if not root.left.left and not root.left.right:   #當前節點的左節點不存在左右子樹
                return root.left.val
            else:
                return self.sumOfLeftLeaves(root.left)
        sum = 0
        if root.left and root.right:        #當前節點既有左子樹又有右子樹
            if not root.left.left and not root.left.right:         #當前節點的左子樹沒有左右節點
                sum += root.left.val
            else:
                sum += self.sumOfLeftLeaves(root.left)
            sum += self.sumOfLeftLeaves(root.right)
        return sum

簡化寫法(參考他人)

def sumOfLeftLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        sum = 0
        if root.left and not root.left.left and not root.left.right:
            sum += root.left.val
        sum += self.sumOfLeftLeaves(root.left)
        sum += self.sumOfLeftLeaves(root.right)
        return sum

2:迭代方法

def sumOfLeftLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        sum = 0
        node = [root]      #存放節點
        while node:
            cur = node.pop(0)         #當前節點
            if not cur.left and not cur.right:   #當前節點無左右子樹
                continue
            elif cur.left and not cur.right:        #當前節點有左子樹無右子樹
                if not cur.left.left and not cur.left.right:     #當前節點的左子樹無左右子樹,滿足左葉子節點條件
                    sum += cur.left.val
                else:
                    node.append(cur.left)
            elif not cur.left and cur.right:          #當前節點無左子樹有右子樹
                node.append(cur.right)
            else:        #當前節點有左右子樹
                if not cur.left.left and not cur.left.right:       #當前節點的左子樹無左右子樹,滿足左葉子節點條件
                    sum += cur.left.val
                else:
                    node.append(cur.left)
                node.append(cur.right)
        return sum

簡化寫法(參考他人)

def sumOfLeftLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        sum = 0
        node = [root]      #存放節點
        while node:
            cur = node.pop(0)         #當前節點
            if cur.left:
                if not cur.left.left and not cur.left.right:
                    sum += cur.left.val
                else:
                    node.append(cur.left)
            if cur.right:
                node.append(cur.right)
        return sum