Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
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 *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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