Diagonal Traversal of Binary Tree | Practice | GeeksforGeeks

Given a Binary Tree, print the diagonal traversal of the binary tree.
Consider lines of slope -1 passing between nodes. Given a Binary Tree, print all diagonal elements in a binary tree belonging to same line.
Example 1:
Input :
            8
         /     \
        3      10
      /   \      \
     1     6     14
         /   \   /
        4     7 13
Output : 8 10 14 3 6 7 13 1 4
Explanation:

Diagonal Traversal of binary tree : 
 8 10 14 3 6 7 13 1 4

Your Task:
You don't need to read input or print anything. The task is to complete the function diagonal() that takes the root node as input argumets and returns the diagonal traversal of the given tree.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Here N is number of nodes.
Constraints:
1 <= Number of nodes<= 105
1 <= Data of a node<= 105
 
0
aishapervin032 weeks ago
vector<int> diagonal(Node *root)
{
    vector<int>ans;
   queue<Node*>q;
   
   
   q.push(root);
   while(q.size()>0)
   {
       Node* t=q.front();
       q.pop();
      
       while(t!=NULL)
       {
           ans.push_back(t->data);
                if(t->left)
                    q.push(t->left);
              
         
           t=t->right;
       }
   }
   return ans;
}
+1
moaslam8263 weeks ago
C++ SOLN
vector<int> diagonal(Node *root)
{
    vector<int> v;
   if(!root) return v;
   queue<Node*> q;
   q.push(root);
   while(!q.empty()){
       Node* temp=q.front();
       q.pop();
       while(temp){
           v.push_back(temp->data);
           if(temp->left) q.push(temp->left);
           temp=temp->right;
       }
   }
   return v; 
}
0
charushilabhadane1 month ago
Simpe Java Solution
  public ArrayList<Integer> diagonal(Node root)     {       ArrayList<Integer>ans=new ArrayList<Integer>();       Queue<Node>q=new LinkedList<Node>();       if(root==null){           return ans;       }       q.offer(root);       while(q.isEmpty()!=true){           Node t=q.peek();           q.poll();           while(t!=null){                if(t.left!=null){               q.offer(t.left);                }               ans.add(t.data);               t=t.right;           }       }       return ans;     }
-1
darsisriguruaakash1 month ago
def diagonal(root):
   #:param root: root of the given tree.
   #return: print out the diagonal traversal,  no need to print new line
   #code here
   q = deque()
   d = {}
   if root is None:
       return 
   hd = 0
   q.appendleft((root,hd))
   while q:
       node,hd = q.pop()
       d.setdefault(hd,[]).append(node.data)
       if node.left is not None :
           q.appendleft((node.left,hd+1))
       if node.right is not None :
           q.appendleft((node.right,hd))
   
   main = []
   for key,values in enumerate(sorted(d)):
       for i in d[values]:
           main.append(i)
   return main
What is wrong in this code ......
+4
arpita252 months ago
vector<int> diagonal(Node *root)
{
   // your code here
   vector<int> v;
   queue<Node*> q;
   Node* p;
   q.emplace(root);
   while(!q.empty())
   {
       p = q.front();
       q.pop();
       while(p)
       {
           if(p->left) q.emplace(p->left);
           v.push_back(p->data);
           p=p->right;
       }
   }
   return v;
}
0
sfazal0002 months ago
 
//easy understandable
//easy understandable
vector<int> diagonal(node* root){   queue<node *> q;   vector<int > ans;      if(root == NULL)   {       return ans;   }      q.push(root);      while(!q.empty())   {       node* t = q.front();       q.pop();              while(t)       {           if(t->left)              q.push(t->left);                          ans.push_back(t->data);                          t = t->right;       }   }         return ans;}       
0
harshladdha042 months ago
 vector<int> diagonal(Node *root)
    {
        vector<int> v;
        queue<Node*> right;
        queue<Node*> left;
        Node* curr;
        curr = root;
        right.push(curr);
        if(curr->left!=NULL)
        left.push(curr->left);
        while(!left.empty() || curr->right!=NULL){
            if(curr->right!=NULL){
                curr=curr->right;
                right.push(curr);
                if(curr->left!=NULL)
                left.push(curr->left);
            }
            else{
                curr = left.front();
                left.pop();
                right.push(curr);
                if(curr->left!=NULL)
                left.push(curr->left);
            }
        }
        while(!right.empty()){
            Node* temp = right.front();
            v.push_back(temp->data);
            right.pop();
        }
        
        return v;
        
        
    	// Code here
    }
0
detroix072 months ago
vector<int> diagonal(Node *root){   queue<Node*> q;   q.push(root);   q.push(NULL);   vector<int> v;      while(q.size()!=0){    Node* temp = q.front();    if(temp==NULL){     q.pop();     if(q.size()==0) break;     else q.push(NULL);    } else {      while(temp!=NULL){      v.push_back(temp->data);      if(temp->left) q.push(temp->left);      temp=temp->right;     }     q.pop();    }   }
   return v;}
+1
vishnuranjan6172 months ago
 
/*       12 line solution
T.C---O(N)   we are traversing every node
S.C---O(N)    we are traversing every element in queue
*/
 vector<int> ans;   if(root==NULL) return ans;   queue<Node*> q;   q.push(root);   while(!q.empty()){     Node* temp=q.front();       q.pop();       while(temp){           if(temp->left) q.push(temp->left);           ans.push_back(temp->data);           temp=temp->right;       }   }   return ans;       
0
prabhnoorsingh04112 months ago
vector<int> diagonal(Node *root){  vector<int> ans;  if(root==NULL){      return ans;  }  queue<Node*> q;  q.push(root);  while(!q.empty())  {      Node *frontnode= q.front();      q.pop();      while(frontnode!=NULL)      {          ans.push_back(frontnode->data);          if(frontnode->left)          {              q.push(frontnode->left);          }          frontnode= frontnode->right;                        }    }  return ans;  } 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.