Inorder Traversal | Practice | GeeksforGeeks

Given a Binary Tree, find the In-Order Traversal of it.
Example 1:
Input:
       1
     /  \
    3    2
Output: 3 1 2


Example 2:
Input:
        10
     /      \ 
    20       30 
  /    \    /
 40    60  50
Output: 40 20 60 10 50 30


Your Task:
You don't need to read input or print anything. Your task is to complete the function inOrder() that takes root node of the tree as input and returns a list containing the In-Order Traversal of the given Binary Tree.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= Number of nodes <= 105
0 <= Data of a node <= 105
 
0
shubham2110199750 minutes ago
static ArrayList<Integer> arr=new ArrayList<Integer>();  static void helper(Node root){      if(root==null) return;      helper(root.left);      arr.add(root.data);      helper(root.right);  }   ArrayList<Integer> inOrder(Node root) {      helper(root);      ArrayList<Integer> arr1=new ArrayList<Integer>();      for(int i=0;i<arr.size();i++){          arr1.add(arr.get(i));      }      arr.clear();        return arr1;   }
0
superrhitik4583 days ago
class Solution { public:   // Function to return a list containing the inorder traversal of the tree.  vector<int> vi;void in(Node* root){    if(root!=NULL){    in(root->left);      vi.push_back(root->data); in(root->right);} return;}vector <int> inOrder(Node* root){ // Your code herein(root); vector<int> res=vi;vi.clear();return res;}};
0
harshbhan261 week ago
class Solution { public:   // Function to return a list containing the inorder traversal of the tree.    vector<int>ans;   vector<int> inOrder(Node* root) {             if(root!=NULL){           inOrder(root->left);           ans.push_back(root->data);           inOrder(root->right);       }              return ans;   }};
+1
itachinamikaze2211 week ago
ArrayList<Integer> inOrder(Node root) {   ArrayList<Integer>res= new ArrayList<>();   Stack<Node> st= new Stack<Node>();   Node n=root;   while(true)   {       if(n!=null)       {           st.push(n);           n=n.left;       }       else       {           if(st.isEmpty())               break;           n=st.pop();           res.add(n.data);           n=n.right;       }   }   return res;}
0
adivp72 weeks ago
void inorder(Node* node, vector<int>& r)
    {
        if(node)
        {
            inorder(node->left,r);
            r.push_back(node->data);
            inorder(node->right,r);
        }
    }
    vector<int> inOrder(Node* root) {
        vector<int> r;
        inorder(root,r);
        return r;
    }
-1
ykc070814 weeks ago
C Solution
int arr[100000];

void inorder(struct Node* root, int* i)
{
    if(root == NULL)
        return;
    inorder(root->left,i);
    arr[(*i)]=root->data;
    (*i) = (*i)+1;
    inorder(root->right,i);
}

int* inOrder(struct Node* root) 
{
    int ind=0;
    inorder(root,&ind);
    return arr;
}
0
bsbh20is1 month ago
int arr[100000];void inorder(struct Node *node,int *i){   if(node==NULL)     return;   inorder(node->left,i);     arr[(*i)]=node->data;   (*i)=(*i)+1;   inorder(node->right,i);   }
int* inOrder(struct Node* root) {   // code here   int ind=0;   inorder(root,&ind);      return arr; }
0
subhashishde081 month ago
class Solution {
  public:
    // Function to return a list containing the inorder traversal of the tree.
    void helper(Node* root,vector<int> &vc){
        if(root==NULL) return;
        helper(root->left,vc);
        vc.push_back(root->data);
        helper(root->right,vc);
    }
    vector<int> inOrder(Node* root) {
        // Your code here
        vector<int> vc;
        helper(root,vc);
        return vc;
    }
};
+1
chamoliabhishek0071 month ago
       res=[]       if not root:           return []       res+=self.InOrder(root.left)       res.append(root.data)       res+=self.InOrder(root.right)       return res
 
0
jyotikaushal3351 month ago
def InOrder(self,root):       # code here       if root is None:          return []              stack = []       result = []              while root is not None or stack != []:           while root is not None:               stack.append(root)               root = root.left                      root = stack.pop()           result.append(root.data)           root = root.right       return result
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.