[LeetCode&Python] Problem 404. Sum of Left Leaves

Find the sum of all left leaves in a given binary tree.

Example:

    3
   /   9  20
    /     15   7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
# 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 sumOfLeftLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        # Recursion method
        ‘‘‘
        def rer(node,left):
            if not node:
                return 0
            if left:
                if node.left or node.right:
                    return rer(node.left,True)+rer(node.right,False)
                else:
                    return node.val
            else:
                return rer(node.right,False)+rer(node.left,True)

        return rer(root,False)
        ‘‘‘
        # Non-recursion method
        ans=0
        if root:
            stack=[root]

            while stack:
                node=stack.pop()
                if node.left:
                    if not node.left.left and not node.left.right:
                        ans+=node.left.val
                    else:
                        stack.append(node.left)
                if node.right:
                    stack.append(node.right)
        return ans

  

原文地址:https://www.cnblogs.com/chiyeung/p/10053015.html

时间: 2024-10-16 13:32:32

[LeetCode&Python] Problem 404. Sum of Left Leaves的相关文章

LeetCode 404. Sum of Left Leaves (左子叶之和)

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 题目标签:Tree 这道题目给了我们一个二叉树,让我们找到所有左子叶之和.这里需要另外一个function - sumLeftLeaves 还要一个int

[Leetcode][Python]40: Combination Sum II

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 40: Combination Sum IIhttps://oj.leetcode.com/problems/combination-sum-ii/ Given a collection of candidate numbers (C) and a target number (T),find all unique combinations in C where the candi

404. Sum of Left Leaves

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 思路:到leaf的时候判断是不是left leaf.用一个boolean function来记录搜索的是左枝还是右枝. /** * Definition

404. Sum of Left Leaves (Easy)

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 题意:计算所有左叶子的和思路:遍历二叉树 1.检查当前节点的左子节点是否是左子叶:2.若是叶子节点,则返回左子叶的值加上对当前结点的右子节点调用递归的结果

[LeetCode&Python] Problem 682. Baseball Game

You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 following types: Integer (one round's score): Directly represents the number of points you get in this round. "+" (one round's score): Represents

[LC] 404. Sum of Left Leaves

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. Solution 1:BFS /** * Definition for a binary tree node. * public class TreeNo

[Leetcode][Python]39: Combination Sum

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 39: Combination Sumhttps://oj.leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a target number (T),find all unique combinations in C where the candidate numbers

404 Sum of Left Leaves 左叶子之和

计算给定二叉树的所有左叶子之和.示例:    3   / \  9  20    /  \   15   7在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24. 详见:https://leetcode.com/problems/sum-of-left-leaves/description/ C++: 方法一: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *le

[LeetCode&Python] Problem1: Two Sum

Problem Description: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given