Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Binary Tree Level Order Traversal II problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Binary Tree Level Order Traversal II problem solution, we have given the root of a binary tree, return the bottom-up level order traversal of its nodes’ values.

Leetcode Binary Tree Level Order Traversal II problem solution

Leetcode Binary Tree Level Order Traversal II problem solution in Python.

class Solution:
    def levelOrderBottom(self, root):
        result = []
        if root == None:
            return result
        def bfsbottomup(result, prev):
            if prev == []:
                return
            temp = []
            cur = []
            for node in prev:
                temp.append(node.val)
                if node.left != None:
                    cur.append(node.left)
                if node.right != None:
                    cur.append(node.right)
            bfsbottomup(result, cur)
            result.append(temp)
        bfsbottomup(result, [root])
        return result

Binary Tree Level Order Traversal II problem solution in Java.

class Solution {
    LinkedList ans = new LinkedList();
    Queue<TreeNode> q = new ArrayDeque();
    
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        
        if(root == null) return ans;
        
        q.offer(root);
        while(!q.isEmpty()){
            
            List <Integer>tmp = new ArrayList();
            
            int size = q.size();
            for(int i = 0 ; i < size; i++){
                
                TreeNode node = q.poll();
                tmp.add(node.val);
                
                if(node.left != null){
                    q.add(node.left);   
                }
                if(node.right != null){
                    q.add(node.right);
                }
            }
            ans.addFirst(tmp);
        }
        
        return ans;
    }
}

Problem solution in C++.

vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> result;
        queue<pair<TreeNode*,int>> toVisit;
        toVisit.push(make_pair(root,0));
        while(!toVisit.empty()) {
            TreeNode *atNode = toVisit.front().first;
            int level = toVisit.front().second;
            toVisit.pop();
            
            if(!atNode) {
                continue;
            }

            if(result.size() <= level) {
                vector<int> empty;
                result.push_back(empty);
            }
            
            result[level].push_back(atNode->val);
            toVisit.push(make_pair(atNode->left,level+1));
            toVisit.push(make_pair(atNode->right,level+1));
        }
        for(int i=0; i<result.size()/2; i++) {
            vector<int> temp = result[i];
            result[i] = result[result.size()-i-1];
            result[result.size()-i-1] = temp;
        }
        return result;
    }

Problem solution in C.

int maxDepth(struct TreeNode* root) {
    if (root==NULL) return 0;
    else {
        int L=maxDepth(root->left);
        int R=maxDepth(root->right);
        int M=(L>R)?L:R;
        
        return M+1;
    }
}

int** levelOrderBottom(struct TreeNode* root, int** columnSizes, int* returnSize) {
    if (root==NULL) return NULL;
    int depth=*returnSize=maxDepth(root);
    
    int** res=(int**)calloc(depth,sizeof(int*));
    *columnSizes=(int*)calloc(depth,sizeof(int));
    
    struct TreeNode* queue[5000];
    int front=0,rear=0;
    queue[rear++]=root;
    int cur_size=1;int next_size=0;int size=depth-1;

    while (front<rear) {
        res[size]=(int*)malloc(2000*sizeof(int));
        
        for (int i=0;i<cur_size&&front<rear;i++) {
            struct TreeNode* temp=queue[front++];
            res[size][i]=temp->val;
            
            if (temp->left) {
                queue[rear++]=temp->left;
                next_size++;
            }
            if (temp->right) {
                queue[rear++]=temp->right;
                next_size++;
            }
        }
        
        (*columnSizes)[size--]=cur_size;

        cur_size=next_size;
        if (cur_size==0) break;
        next_size=0;
    }
    
    return res;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes