Python3解leetcode Same TreeBinary 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,null,null,15,7],

    3
   /   9  20
    /     15   7

return its bottom-up level order traversal as:

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

注意是每一层的所有数字放入同一个list内

思路:二叉树问题,考虑使用递归算法,计算出每一层的所有元素值

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        if root == None: return []
        def order(rootx,level):
            if rootx == None:return
            if (level) == len(result):
                result.append([rootx.val])
            else:
                result[level].append(rootx.val)
            order(rootx.left,level+1)
            order(rootx.right,level+1)

        result = []
        order(root,0)
        return result[::-1]   

原文地址:https://www.cnblogs.com/xiaohua92/p/11072788.html

时间: 2024-08-28 22:08:02

Python3解leetcode Same TreeBinary Tree Level Order Traversal II的相关文章

【Leetcode】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}, 3 / 9 20 / 15 7 return its bottom-up level order trave

Leetcode 树 Binary Tree Level Order Traversal II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Level Order Traversal II Total Accepted: 10080 Total Submissions: 32610 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, l

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

题目: 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 tr

Java for LeetCode 107 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}, 3 / 9 20 / 15 7 return its bottom-up level order trave

leetCode 107. Binary Tree Level Order Traversal II 二叉树层次遍历反转

107. 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,null,null,15,7],     3  

leetcode No107. Binary Tree Level Order Traversal II

Question: 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,null,null,15,7], 3 / 9 20 / 15 7 return its bottom-up l

(二叉树 BFS) leetcode 107. 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,null,null,15,7], 3 / 9 20 / 15 7 return its bottom-up level order

Java [Leetcode 107]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}, 3 / 9 20 / 15 7 return its bottom-up level order

leetCode 107.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}, 3 / 9 20 / 15 7 return its bottom-up level order trave