-
Notifications
You must be signed in to change notification settings - Fork 1
/
Height_of_Binary_Tree.cpp
60 lines (51 loc) · 1.01 KB
/
Height_of_Binary_Tree.cpp
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
59
60
/*
Problem Statement:
-----------------
Given a binary tree, find its height.
Example 1:
---------
Input:
1
/ \
2 3
Output: 2
Example 2:
---------
Input:
2
\
1
/
3
Output: 3
Your Task:
You don't need to read input or print anything. Your task is to complete the function height() which takes root node of the tree as input parameter and returns
an integer denoting the height of the tree. If the tree is empty, return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N
*/
// Link --> https://practice.geeksforgeeks.org/problems/height-of-binary-tree/1#
// Code:
/*
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node(int x){
data = x;
left = right = NULL;
}
};
*/
class Solution
{
public:
//Function to find the height of a binary tree.
int height(struct Node* node)
{
if(node == NULL)
return 0;
return(max (height(node->left) , height(node->right)) + 1);
}
};