# Binary Tree Level Order Traversal II Leetcode Solution

In this post, we are going to solve the Binary Tree Level Order Traversal II Leetcode SolutionÂ problem of Leetcode. This Leetcode problem is done in many programming languages like C++, Java, and Python.

## Problem

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

Now, letâ€™s see the leetcode solution ofÂ Binary Tree Level Order Traversal II Leetcode Solution.

### Binary Tree Level Order Traversal II Leetcode Solution in Python

class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []

ans = []
q = deque([root])

while q:
currLevel = []
for _ in range(len(q)):
node = q.popleft()
currLevel.append(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
ans.append(currLevel)

return ans[::-1]

### Binary Tree Level Order Traversal II Leetcode Solutionin CPP

class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
if (root == nullptr)
return {};

vector<vector<int>> ans;
queue<TreeNode*> q{{root}};

while (!q.empty()) {
vector<int> currLevel;
for (int sz = q.size(); sz > 0; --sz) {
TreeNode* node = q.front();
q.pop();
currLevel.push_back(node->val);
if (node->left)
q.push(node->left);
if (node->right)
q.push(node->right);
}
ans.push_back(currLevel);
}

reverse(begin(ans), end(ans));
return ans;
}
};

### Binary Tree Level Order Traversal II Leetcode Solution in Java

class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
if (root == null)
return new ArrayList<>();

List<List<Integer>> ans = new ArrayList<>();
Queue<TreeNode> q = new ArrayDeque<>(Arrays.asList(root));

while (!q.isEmpty()) {
List<Integer> currLevel = new ArrayList<>();
for (int sz = q.size(); sz > 0; --sz) {
TreeNode node = q.poll();
if (node.left != null)
q.offer(node.left);
if (node.right != null)
q.offer(node.right);
}