[Leetcode]@python 107. Binary Tree Level Order Traversal II

题目链接

https://leetcode.com/problems/binary-tree-level-order-traversal-ii/

题目原文

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree {3,9,20,#,#,15,7},

return its bottom-up level order traversal as:

题目大意

自下而上层次遍历二叉树

解题思路

用之前层次遍历二叉树的算法,最后翻转一下

代码

# 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 help(self, root, level, ans):
        if root:
            if len(ans) < level + 1:
                ans.append([])
            ans[level].append(root.val)
            self.help(root.left, level + 1, ans)
            self.help(root.right, level + 1, ans)

    def levelOrderBottom(self, root):
        """
 :type root: TreeNode
 :rtype: List[List[int]]
 """
        ans = []
        self.help(root, 0, ans)
        ans.reverse()
        return ans
        

你可能感兴趣的:([Leetcode]@python 107. Binary Tree Level Order Traversal II)