Binary Tree Level Order Traversal – Leetcode Solution

In this post, we are going to solve the problem, 102. Binary Tree Level Order Traversal problem of Leetcode. This problem 102. Binary Tree Level Order Traversal is a Leetcode medium level problem. Let’s see the code, 102. Binary Tree Level Order Traversal – Leetcode Solution.

Problem

Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).

Example 1 :

Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]

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 code of 102. Binary Tree Level Order Traversal – Leetcode Solution.

Diameter of Binary Tree – Leetcode Solution

102. Binary Tree Level Order Traversal – Solution in Java

/**
 * 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>> levelOrder(TreeNode root) {
        ArrayDeque<TreeNode> q = new ArrayDeque<TreeNode>();
        List<List<Integer>> ans = new ArrayList<>();
        if(root == null)return ans;
        q.add(root);
        while(q.size()>0){
            
            List<Integer> subAns = new ArrayList<Integer>();
            int size = q.size();
            while(size-- > 0){
                if(q.peek().left != null) q.add(q.peek().left);
                if(q.peek().right != null) q.add(q.peek().right);
                subAns.add(q.remove().val);
            }
            ans.add(subAns);
        }
        return ans;
    }
}

102. Binary Tree Level Order Traversal – Solution in C++

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        if (!root) { return {}; }
        vector<int> row;
        vector<vector<int> > result;
        queue<TreeNode*> q;
        q.push(root);
        int count = 1;

            while (!q.empty()) {
            if (q.front()->left) { q.push(q.front()->left); }
            if (q.front()->right) { q.push(q.front()->right); }
            row.push_back(q.front()->val), q.pop();
            if (--count == 0) {
                result.emplace_back(row), row.clear();
                count = q.size();
            }
        }
        return result;
    }
};

102. Binary Tree Level Order Traversal – Solution in Python

from collections import deque
class Solution:
    def levelOrder(self, root):
        if not root: return []
        queue, res = deque([root]), []
        
        while queue:
            cur_level, size = [], len(queue)
            for i in range(size):
                node = queue.popleft()
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
                cur_level.append(node.val)
            res.append(cur_level)
        return res

Note: This problem 102. Binary Tree Level Order Traversal is generated by Leetcode but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

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