Binary Tree Level Order Traversal @LeetCode

PROBLEM :

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],
    3
   / \
  9  20
    /  \
   15   7
 
return its level order traversal as:
[
  [3],
  [9,20],
  [15,7]
]

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Recursive Solution )
--------------------------------------------------------------------------------

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        int height=FindHeight(root) ;
       
        vector<vector<int>> vec(height,vector<int> {});
        LevelTraversal(root,vec,0) ;
        return vec ;
    }
   
    int FindHeight(TreeNode* root){
        if(root==NULL)
            return 0 ;
           
        int l=FindHeight(root->left) ;
        int r=FindHeight(root->right) ;
        return l>r?l+1:r+1 ;
    }
   
    void LevelTraversal(TreeNode* root,vector<vector<int>> &vec,int curr){
        if(root==NULL)
            return ;
           
        vec[curr].push_back(root->val);
           
        LevelTraversal(root->left,vec,curr+1) ;
        LevelTraversal(root->right,vec,curr+1) ;
    }
};

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Iterative Solution - QUEUE )
--------------------------------------------------------------------------------

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        int height=FindHeight(root) ;
        vector<vector<int>> vec(height,vector<int> {});
        if(height==0)
            return vec ;
           
        int curr=0 ;
        queue<TreeNode*> que ;
        que.push(root) ;
       
        while(!que.empty()){
            int size=que.size() ;
            while(size--){
                TreeNode* temp ;
                temp=que.front() ;
                que.pop() ;
           
                vec[curr].push_back(temp->val);
               
                if(temp->left)
                    que.push(temp->left) ;
                if(temp->right)
                    que.push(temp->right) ;
            }
            curr++ ;
        }
        return vec ;
    }
   
    int FindHeight(TreeNode* root){
        if(root==NULL)
            return 0 ;
           
        int l=FindHeight(root->left) ;
        int r=FindHeight(root->right) ;
        return l>r?l+1:r+1 ;
    }
};

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )