Tree: Level Order Traversal

Sort by

recency

|

573 Discussions

|

  • + 0 comments
    void levelOrder(Node * root) {
    
        queue<Node*>q;
        q.push(root);
        while(!q.empty()){
            int size = q.size(); 
            for(int i=0;i<size;i++){
    
              Node* popnode=q.front();
              q.pop();
              cout<<popnode->data<<" ";
              if(popnode->left!=NULL){
                  q.push(popnode->left);
              }
              if(popnode->right!=NULL){
                 q.push(popnode->right);
              }
            }
    
        }
    
    
    }
    
  • + 0 comments

    My C code 😎😁

    int height(struct node* root){
        if(root == NULL){
            return 0;
        }else{
            const int leftHeight = height(root->left);
            const int rightHeight = height(root->right);
            if(leftHeight > rightHeight){
                return (leftHeight + 1);
            }else{
                return (rightHeight + 1);
            }
        }
    }
    
    void printCurrentLevelOrder(struct node* root,int level){
        if(root == NULL){
            return;
        }
        if(level == 1){
            printf("%d ",root->data);
        }else if(level > 1){
            printCurrentLevelOrder(root->left,level - 1);
            printCurrentLevelOrder(root->right,level - 1);
        }
    }
    
    void levelOrder( struct node *root) {
        int h = height(root);
        for(int i = 1;i <= h;i++){
            printCurrentLevelOrder(root,i);
        }
    }
    
  • + 0 comments
         public static void levelOrder(Node root) {
            Queue<Node> queue = new LinkedList<>();
            queue.add(root);
            while(!queue.isEmpty()) {
                Node current = queue.poll();
                System.out.print(current.data+" ");
                if(current.left != null) queue.add(current.left);
                if(current.right != null) queue.add(current.right);
            }
    
        }
    
  • + 0 comments

    My C++ Implementation

    queue<Node*> Q;
        Q.push(root);
    
        while (!Q.empty()) {
    
    
                Node *current = Q.front();
    
               cout<<current->data<<" ";
    
                if(current->left != nullptr)
                {
                    Q.push(current->left);
                }
                if(current->right != nullptr)
                {
                    Q.push(current->right);
                }
                Q.pop();
    
        }
    
  • + 0 comments
    void topView(Node* root) {
        std::queue<std::pair<int, Node*>> q;
        q.push(std::make_pair(0,root));
    
        while (!q.empty())
        {
            auto i = q.front();
            if(i.second != nullptr)
            {
                q.push(std::make_pair(i.first-1, i.second -> left));
                q.push(std::make_pair(i.first+1, i.second -> right));
                std::cout<<i.second->data<<' ';    
            }
            q.pop();
            i=q.front();
    
        }
    
        
    }