[LeetCode] 107. Binary Tree Level Order Traversal II
Given the root of a binary tree, return the bottom-up level order traversal of its nodes’ values. (i.e., from left to right, level by level from leaf to root).
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: [[15,7],[9,20],[3]]
Example 2:
Input: root = [1]
Output: [[1]]
Example 3:
Input: root = []
Output: []
Constraints:
The number of nodes in the tree is in the range [0, 2000].
-1000 <= Node.val <= 1000
二叉树的层序遍历 II。
给你二叉树的根节点 root ,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
思路
这道题跟102题唯一的区别是每一层的节点整理好以后,是加到结果集的头部而不是尾部。这里我只提供 BFS 实现。
复杂度
时间O(n)
空间O(n^2)
代码
Java实现
1 |
|
相关题目
1 |
|
[LeetCode] 107. Binary Tree Level Order Traversal II
https://shurui91.github.io/posts/2469883664.html