Rumah  >  Soal Jawab  >  teks badan

Python 求二叉树所有左叶节点的和

python# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution(object):
    def sumOfLeftLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root is None or self.isleaf(root):            return 0
        if self.isleaf(root.left):            return root.left.val + self.sumOfLeftLeaves(root.right)        else:            return self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)        
    def isleaf(self,root):
        return root.left is None and root.left is None

原题地址是 https://leetcode.com/problems/sum-of-left-leaves/ 求解,这段代码哪里写错了?


巴扎黑巴扎黑2898 hari yang lalu840

membalas semua(1)saya akan balas

  • 面对疾风吧

    面对疾风吧2016-11-11 16:22:37

    按我的理解, left leaves != left node 

    ~~~python 
    def is_leaf(self, node): 
    return node is not None and node.left is None and node.right is None

    balas
    0
  • Batalbalas