zoukankan      html  css  js  c++  java
  • 404. Sum of Left Leaves

    https://leetcode.com/problems/sum-of-left-leaves/#/description

    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.


    Hint:

    Q: How to define if a node is a left leaf?

    A: When the node exists and it has no right or left child.

    See node 9 and 15 in the given example.

    i.e. if root.left and not root.left.left and not root.left.right (== True)

    It means the root.left is the left leaf of root. 

    Sol1: 

    Recursion. (I prefer recursion more than iteration.)

    # 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
            
            # base case: node is none
            if root is None:
                return 0
            # recursive case: left child is/isn't child 
            if root.left and not root.left.left and not root.left.right:
                return root.left.val + self.sumOfLeftLeaves(root.right)
            return self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)

    Note:

    1 When we find a left leaf, we should return the sum of its value and the right half of the root.  That's where recursion comes into play. 

    2 The final return result should be the sum of all values of the left subtrees and all right subtrees. Do it recursively.

    3 Don't need to use while loop to advance nodes in recursion.  

    Sol2:

    Recursion.

    In this solution, we use varible to store sum of left leaf values. Easier to understand.

    # 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
            
            # base case: node is none
            sum = 0
            if root is None:
                return 0
            # recursive case: left child is/isn't child 
            if root.left and not root.left.left and not root.left.right:
                sum += root.left.val 
            sum += self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)
            return sum

    Note:

    1 "sum += root.left.val " stores values of left leaves in left subtress. No need to return anything.

    "sum += self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)" returns all branches of the tree. It will be returned. 

    Sol3:

    Iteration using stack. (It is more efficient to use recursion!)

    It's basically the mutation of traversing the tree using iteration(stack). Not recommended.  Refer here to see more ways to implement tree traversal.

    # 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
            """
            # iteration
            #  traverse the whole tree using stack. If the left child is leave, add it to the result.
            
            if not root:
                return 0
            s = [root]
            res = 0
            while s:
                tmp = s.pop()
                if tmp.left:
                    s.append(tmp.left)
                    if not tmp.left.left and not tmp.left.right:
                        res += tmp.left.val
                if tmp.right:
                    s.append(tmp.right)
            return res

     

     

     

    
    
  • 相关阅读:
    笔记04_正确使用Heterogeneous元件
    java网络通信:伪异步I/O编程(PIO)
    java网络通信:异步非阻塞I/O (NIO)
    lua源码学习篇二:语法分析
    lua源码学习篇三:赋值表达式解析的流程
    java网络通信:netty
    lua源码学习篇一:环境部署
    lua源码学习篇四:字节码指令
    java网络通信:同步阻塞式I/O模型(BIO)
    前端项目开发流程
  • 原文地址:https://www.cnblogs.com/prmlab/p/6964248.html
Copyright © 2011-2022 走看看