-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path55_binaryTree_depthOfTree.cpp
More file actions
58 lines (51 loc) · 1.2 KB
/
55_binaryTree_depthOfTree.cpp
File metadata and controls
58 lines (51 loc) · 1.2 KB
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
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node *left;
node *right;
node(int d){
this->data = d;
this->left = NULL;
this->right = NULL;
}
};
node* buildingTree(node* root){
cout << "Enter the data: " << endl;
int data;
cin >> data;
root = new node(data);
if(data == -1)
return NULL;
cout << "Enter the data inserting in left: " << data << endl;
root->left = buildingTree(root->left);
cout << "Enter the data inserting in right: " << data << endl;
root->right = buildingTree(root->right);
return root;
}
void inOrder(node* root){
if(root == NULL){
return;
}
inOrder(root->left);
cout << root->data << " ";
inOrder(root->right);
}
int maxDepth(node* root){
if(root == NULL){
return 0;
}
int left = maxDepth(root->left);
int right = maxDepth(root->right);
int ans = max(left, right) + 1;
return ans;
}
int main(){
node *root = NULL;
// 1 3 7 - 1 - 1 11 - 1 - 1 5 17 - 1 - 1 - 1
root = buildingTree(root);
// inOrder(root);
int ans = maxDepth(root);
cout << "Maximum depth of tree is: " << ans << endl;
}