def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []
        a = [root]
        r=[]
        while len(a):
            r.append(list(map(lambda x: x.val, a)))
            b = []
            for i in xrange(0, len(a)):
                if a[i].left:
                    b.append(a[i].left)
                if a[i].right:
                    b.append(a[i].right)
            a = b
        r.reverse()
        return r






if __name__ == '__main__':
    s = Solution()
    import util
    root = util.constructTree("3,9,20,#,#,15,7")
    s.levelOrderBottom(root)
示例#2
0
# 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 hasPathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool
        """
        if not root:
            return False
        sum -= root.val
        if sum == 0:
            if (not root.left) and (not root.right):
                return True

        return self.hasPathSum(root.left, sum) or self.hasPathSum(
            root.right, sum)


if __name__ == '__main__':
    s = Solution()
    import util
    root = util.constructTree("1,-2,-3,1,3,-2,#,-1")
    s.hasPathSum(root, -1)
        self.right = None


class Solution(object):
    def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []
        a = [root]
        r = []
        while len(a):
            r.append(list(map(lambda x: x.val, a)))
            b = []
            for i in xrange(0, len(a)):
                if a[i].left:
                    b.append(a[i].left)
                if a[i].right:
                    b.append(a[i].right)
            a = b
        r.reverse()
        return r


if __name__ == '__main__':
    s = Solution()
    import util
    root = util.constructTree("3,9,20,#,#,15,7")
    s.levelOrderBottom(root)
示例#4
0
__author__ = 'feng'
# 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 hasPathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool
        """
        if not root:
            return False
        sum -= root.val
        if sum == 0:
            if (not root.left) and (not root.right):
                return True

        return self.hasPathSum(root.left, sum) or self.hasPathSum(root.right, sum)

if __name__ == '__main__':
    s = Solution()
    import util
    root = util.constructTree("1,-2,-3,1,3,-2,#,-1")
    s.hasPathSum(root, -1)