Binary Tree Level Order Traversal

Write a program to find the Binary Tree Level Order Traversal.

Example 1:

Input:        1
             / \
            4   2
               /  
              3   
Output: [
          [1],
          [4,2],
          [3]
        ]

Approach:

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class LevelOrderTraversal {
    public static void main(String[] args) {
        TreeNode tree = new TreeNode(1);
        tree.left = new TreeNode(4);
        tree.right = new TreeNode(2);
        tree.right.left = new TreeNode(3);
        List<List<Integer>> r = levelOrder(tree);
        System.out.println("Level Order Traversal " + Arrays.asList(r));
    }

    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if (root == null)
            return result;
        Queue<TreeNodeq = new LinkedList<>();
        q.add(root);
        while (!q.isEmpty()) {
            int size = q.size();
            List<Integerr = new ArrayList<>();
            while (size > 0) {
                root = q.peek();
                q.remove();
                r.add(root.val);
                if (root.left != null)
                    q.add(root.left);
                if (root.right != null)
                    q.add(root.right);
                size--;
            }
            result.add(r);
        }
        return result;

    }
}

C++


#include <bits/stdc++.h>
using namespace std;
//struct for treenode
struct TreeNode{
    int data;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int data)
    {
        this->data=data;
        this->left=NULL;
        this->right=NULL;
    }
};
//Function to find the level order
//traversal of the tree
vector<vector<int>> levelOrderTraversal(TreeNode *root)
{
  vector<vector<int>> res;
  if(root==NULL)
      return res;
  queue<TreeNode*> q;
  q.push(root);
  //Run loop till queue is not empty
  while(!q.empty())
    {
         int len=q.size();
         vector<intvec;
         while(len--)
         {
           root=q.front();
           q.pop();
           vec.push_back(root->data);
             if(root->left)
                q.push(root->left);
             if(root->right)
                q.push(root->right);
         } 
         res.push_back(vec);
    }
    return res;
}
int main()
{
    TreeNode *tree=new TreeNode(1);
    tree->left=new TreeNode(4);
    tree->right=new TreeNode(2);
    tree->right->left=new TreeNode(3);
    cout<<"Level Order Traversal\n";
    vector<vector<int>> res=levelOrderTraversal(tree);
    int n=res.size();
    cout<<"[\n";
    for(int i=0;i<n;i++)
       {
           cout<<" [";
           for(int j=0;j<res[i].size();j++)
              {
                  cout<<res[i][j];
                  if(j!=res[i].size()-1)
                     cout<<",";
              }
            cout<<"]";
          if(i!=n-1)
          {
              cout<<",";
              cout<<"\n";
          }
       }
    cout<<"\n]";
    return 0;
}
//Time Complexity: O(n)


No comments:

Post a Comment