Binary Tree Level Order Traversal

Binary Tree Level Order Traversal

  • Problem Statement

    Given a binary tree, populate an array to represent its level-by-level traversal. You should populate the values of all nodes of each level from left to right in separate sub-arrays.

    Example 1:
    在这里插入图片描述
    Example 2:
    在这里插入图片描述

  • Code

import java.util.*;

class TreeNode {
  int val;
  TreeNode left;
  TreeNode right;

  TreeNode(int x) {
    val = x;
  }
};

class LevelOrderTraversal {
  public static List<List<Integer>> traverse(TreeNode root) {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    // TODO: Write your code here
    Queue<TreeNode>queue=new LinkedList<>();
    queue.add(root);
    while(!queue.isEmpty()){
      int size=queue.size();
      List<Integer>currentList=new ArrayList<>();
      for(int i=0;i<size;i++){
        TreeNode node=queue.poll();
        currentList.add(node.val);
        if(node.left!=null){
          queue.add(node.left);
        }
        if(node.right!=null){
          queue.add(node.right);
        }
      }
      result.add(currentList);  
    }
    return result;
  }

  public static void main(String[] args) {
    TreeNode root = new TreeNode(12);
    root.left = new TreeNode(7);
    root.right = new TreeNode(1);
    root.left.left = new TreeNode(9);
    root.right.left = new TreeNode(10);
    root.right.right = new TreeNode(5);
    List<List<Integer>> result = LevelOrderTraversal.traverse(root);
    System.out.println("Level order traversal: " + result);
  }
}

  • Solution
    Since we need to traverse all nodes of each level before moving onto the next level, we can use the Breadth First Search (BFS) technique to solve this problem.

    We can use a Queue to efficiently traverse in BFS fashion. Here are the steps of our algorithm:

    1. Start by pushing the root node to the queue.
    2. Keep iterating until the queue is empty.
    3. In each iteration, first count the elements in the queue (let’s call it levelSize). We will have these many nodes in the current level.
    4. Next, remove levelSize nodes from the queue and push their value in an array to represent the current level.
    5. After removing each node from the queue, insert both of its children into the queue.
    6. If the queue is not empty, repeat from step 3 for the next level.

    Let’s take the example-2 mentioned above to visually represent our algorithm:
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • Time complexity
    The time complexity of the above algorithm is O(N), where ‘N’ is the total number of nodes in the tree. This is due to the fact that we traverse each node once.

  • Space complexity
    The space complexity of the above algorithm will be O(N) as we need to return a list containing the level order traversal. We will also need O(N) space for the queue. Since we can have a maximum of N/2 nodes at any level (this could happen only at the lowest level), therefore we will need O(N) space to store them in the queue.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值