1void inorder(Node* root){
2 if(root != NULL){
3 inorder(root->left);
4 cout<<root->data<<" ";
5 inorder(root->right);
6 }
7}
1vector<int> v;
2void inorder(TreeNode* root,vector<int> &v)
3{
4 if(root==NULL)
5 return;
6 inorder(root->left,v);
7 v.push_back(root->val);
8 inorder(root->right,v);
9}
1class Solution {
2 public List<Integer> inorderTraversal(TreeNode root) {
3 List<Integer> list = new ArrayList<Integer>();
4 return dfs(root, list);
5 }
6 private List<Integer> dfs(TreeNode root, List<Integer> list)
7 {
8 if(root == null)
9 return list;
10 list = dfs(root.left, list);
11 list.add(root.val);
12 return dfs(root.right,list);
13 }
14}
1#include <iostream>
2using namespace std;
3
4class node{
5public:
6 int data;
7 node* left;
8 node* right;
9
10 node(int d){
11 data = d;
12 left = NULL;
13 right = NULL;
14 }
15};
16
17node* buildTree(){
18 int d;
19 cin>>d;
20
21 if(d==-1){
22 return NULL; //to attach a NULL pointer[in case of no child] enter -1
23 }
24 node * root = new node(d);
25 root->left = buildTree();
26 root->right = buildTree();
27 return root;
28}
29
30
31
32//REQUIRED FUNCTION: Inorder Traversal
33
34
35void printIn(node*root){
36 if(root==NULL){
37 return;
38 }
39 //Otherwise Left Root Right
40 printIn(root->left);
41 cout<<root->data<<" ";
42 printIn(root->right);
43}
44
45
46int main(){
47 node* root = buildTree();
48 printIn(root);
49
50 return 0;
51}
52
53//SAMPLE INPUT TO RUN THE CODE ON ANY ONLINE IDE:
54//8 10 1 -1 -1 6 9 -1 -1 7 -1 -1 3 -1 14 13 -1 -1 -1
55