Binary Tree Level Order Traversal

ZeeshanAli-0704 - Oct 8 '22 - - Dev Community

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]]

Solution 1 : keeping seperate row & then pushing rows in level

const levelOrder = (root) => {
  // BFS Level Order
  // Learning this method will help with all level order problems, graphs and trees

  if (!root) return []; // Edge case if root is null

  // First we have a queue intialized with the root
  let q = [root];
  let res = []; // our result array

  while (q.length) {
    // BFS continues till the queue is empty
    const len = q.length; // We need a helper variable for our inner level for loop, this helps us only process for this level

    // This loop is for processing all queue items on each LEVEL of the tree
    const levelArray = []; // Used to hold all values from this level
    for (let i = 0; i < len; i++) {
      const n = q.shift(); // We get first item in queue
      levelArray.push(n.val); // Push node value to the levelArray

      // We need to queue up all the children of the current node, if they exist
      n.left && q.push(n.left);
      n.right && q.push(n.right);
      // The above is the same as:
      // if(n.left) q.push(n.left)
    }

    // Once we process all nodes for this level, push levelArray to result array
    res.push(levelArray);
  }

  return res;
};

Enter fullscreen mode Exit fullscreen mode

Solution 2 : keeping same array levels & by default keeping empty & pushing element in empty level

/**
 * 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 levelOrder = function(root) {
 if (!root) return [];
  let queue = [root];
  let levels = [];
  while (queue.length > 0) {
    let levelSize = queue.length;
    levels.push([]);
    for (let i = 0; i < levelSize; i++) {
      let curr = queue.shift();
     levels[levels.length - 1].push(curr.val);

      if (curr.left) queue.push(curr.left);
      if (curr.right) queue.push(curr.right);
    }
  }

  return levels;
};
Enter fullscreen mode Exit fullscreen mode

solution 3: via recurssion

var levelOrder = function(root) {
    let result = [];
    let level = 0;
    const readNode = function(node, level) {
        if (!node) return;
        if (!result[level]) {
            result.push([node.val]);
        } else {
            result[level].push(node.val);
        }
        readNode(node.left, level + 1);
        readNode(node.right, level + 1);
    }
    readNode(root, level);
    return result;
};

Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player