【LeetCode with Python】 107. Binary Tree Level Order Traversal II

题目

原题页面:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
本文地址:http://leetcode.xnerv.wang/binary-tree-level-order-traversal-ii/
题目类型:Tree, Breadth-first Search
难度评价:Easy
类似题目:(E) Binary Tree Level Order Traversal

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},

    3
   / \
  9  20
    /  \
   15   7


return its bottom-up level order traversal as:

[
  [15,7],
  [9,20],
  [3]
]


confused what "{1,#,2,3}" means?
OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:

   1
  / \
 2   3
    /
   4
    \
     5


The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".


分析

不知道出题意图是不是希望用递归,从叶子节点到根结点,将每一个节点加入到结果二维数组的相应层次中?
不过我这里就追求简单,直接将Binary Tree Level Order Traversal的结果数组reverse一下。。。


代码

class Solution:

    # @param root, a tree node
    # @return a list of lists of integers
    def levelOrder(self, root):
        if None == root:
            return [ ]
        results = [ [root.val] ]
        reflist1 = [root]
        while True:
            reflist2 = [ ]
            result = [ ]
            for i in range(0, len(reflist1)):
                cur = reflist1[i]
                if None != cur.left:
                    reflist2.append(cur.left)
                    result.append(cur.left.val)
                if None != cur.right:
                    reflist2.append(cur.right)
                    result.append(cur.right.val)
            if 0 == len(reflist2):
                break
            results.append(result)
            reflist1 = reflist2
        return results

    def levelOrderBottom(self, root):
        results = self.levelOrder(root)
        results.reverse()
        return results

results matching ""

    No results matching ""