-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHight_BST.cpp
More file actions
41 lines (33 loc) · 820 Bytes
/
Hight_BST.cpp
File metadata and controls
41 lines (33 loc) · 820 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
#include <iostream>
#include <algorithm>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int height(TreeNode* root) {
if (root == nullptr)
return 0;
int leftH = height(root->left);
int rightH = height(root->right);
return max(leftH, rightH) + 1;
}
int main() {
/*
Example tree:
1
/ \
2 3
/ \
4 5
*/
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
cout << "Height = " << height(root) << endl;
return 0;
}