-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn_th_node_of_inorder_traversal.cpp
More file actions
53 lines (48 loc) · 1006 Bytes
/
n_th_node_of_inorder_traversal.cpp
File metadata and controls
53 lines (48 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
/*
Input : n = 4
10
/ \
20 30
/ \
40 50
Output : 10
Inorder Traversal is : 40 20 50 10 30
*/
struct Node{
int data;
struct Node *left,*right;
};
struct Node* newNode(int data){
struct Node* node=(struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
void NthInorder(struct Node *root,vector<int> &v)
{
if(root==NULL)
return;
NthInorder(root->left,v);
v.push_back(root->data);
NthInorder(root->right,v);
}
int main()
{
struct Node* root=newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right = newNode(50);
int n = 1;
vector<int> v;
NthInorder(root, v);
if(v.size()<n||n==0)cout<<"Not possible";
else
{
cout<<v.at(n-1);
}
return 0;
}