107. Binary Tree Level Order Traversal II

Description

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

Solutions

Solution 1: BFS

The approach is the same as in 102. Binary Tree Level Order Traversal, just reverse the result in the end.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.

• /**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode() {}
*     TreeNode(int val) { this.val = val; }
*     TreeNode(int val, TreeNode left, TreeNode right) {
*         this.val = val;
*         this.left = left;
*         this.right = right;
*     }
* }
*/
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
if (root == null) {
return ans;
}
q.offerLast(root);
while (!q.isEmpty()) {
List<Integer> t = new ArrayList<>();
for (int i = q.size(); i > 0; --i) {
TreeNode node = q.pollFirst();
if (node.left != null) {
q.offerLast(node.left);
}
if (node.right != null) {
q.offerLast(node.right);
}
}
}
return ans;
}
}

• /**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> ans;
if (!root) return ans;
queue<TreeNode*> q{ {root} };
while (!q.empty()) {
vector<int> t;
for (int i = q.size(); i; --i) {
auto node = q.front();
q.pop();
t.emplace_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
ans.emplace_back(t);
}
reverse(ans.begin(), ans.end());
return ans;
}
};

• # Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
ans = []
if root is None:
return ans
q = deque([root])
while q:
t = []
for _ in range(len(q)):
node = q.popleft()
t.append(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
ans.append(t)
return ans[::-1]


• /**
* Definition for a binary tree node.
* type TreeNode struct {
*     Val int
*     Left *TreeNode
*     Right *TreeNode
* }
*/
func levelOrderBottom(root *TreeNode) [][]int {
ans := [][]int{}
if root == nil {
return ans
}
q := []*TreeNode{root}
for len(q) > 0 {
var t []int
for i := len(q); i > 0; i-- {
node := q[0]
q = q[1:]
t = append(t, node.Val)
if node.Left != nil {
q = append(q, node.Left)
}
if node.Right != nil {
q = append(q, node.Right)
}
}
ans = append([][]int{t}, ans...)
}
return ans
}

• /**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
*     this.val = (val===undefined ? 0 : val)
*     this.left = (left===undefined ? null : left)
*     this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrderBottom = function (root) {
const ans = [];
if (!root) return ans;
const q = [root];
while (q.length) {
const t = [];
for (let i = q.length; i > 0; --i) {
const node = q.shift();
t.push(node.val);
if (node.left) q.push(node.left);
if (node.right) q.push(node.right);
}
ans.unshift(t);
}
return ans;
};


• // Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
//   pub val: i32,
//   pub left: Option<Rc<RefCell<TreeNode>>>,
//   pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
//   #[inline]
//   pub fn new(val: i32) -> Self {
//     TreeNode {
//       val,
//       left: None,
//       right: None
//     }
//   }
// }
use std::{ rc::Rc, cell::RefCell, collections::VecDeque };
impl Solution {
pub fn level_order_bottom(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
if root.is_none() {
return vec![];
}
let mut ret_vec = Vec::new();
let mut q = VecDeque::new();

q.push_back(root);

while !q.is_empty() {
let mut cur_vec = Vec::new();
let mut next_q = VecDeque::new();
while !q.is_empty() {
let cur_front = q.front().unwrap().clone();
q.pop_front();
cur_vec.push(cur_front.as_ref().unwrap().borrow().val);
let left = cur_front.as_ref().unwrap().borrow().left.clone();
let right = cur_front.as_ref().unwrap().borrow().right.clone();
if !left.is_none() {
next_q.push_back(left);
}
if !right.is_none() {
next_q.push_back(right);
}
}
ret_vec.push(cur_vec);
q = next_q;
}

ret_vec.reverse();
ret_vec
}
}


• /**
* Definition for a binary tree node.
* class TreeNode {
*     val: number
*     left: TreeNode | null
*     right: TreeNode | null
*     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
*         this.val = (val===undefined ? 0 : val)
*         this.left = (left===undefined ? null : left)
*         this.right = (right===undefined ? null : right)
*     }
* }
*/

function levelOrderBottom(root: TreeNode | null): number[][] {
const ans: number[][] = [];
if (!root) {
return ans;
}
const q: TreeNode[] = [root];
while (q.length) {
const t: number[] = [];
const qq: TreeNode[] = [];
for (const { val, left, right } of q) {
t.push(val);
left && qq.push(left);
right && qq.push(right);
}
ans.push(t);
q.splice(0, q.length, ...qq);
}
return ans.reverse();
}