Skip to content

Commit 1ef9569

Browse files
Merge pull request #248 from Adi-3108/Adi-3108
Added Code to Calculate Height of Binary Tree in Cpp
2 parents 1d1ebe2 + 889a3ff commit 1ef9569

File tree

2 files changed

+155
-1
lines changed

2 files changed

+155
-1
lines changed

.vscode/settings.json

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,71 @@
5858
"C_Cpp_Runner.msvcSecureNoWarnings": false,
5959
"files.associations": {
6060
"iostream": "cpp",
61-
"string": "cpp"
61+
"string": "cpp",
62+
"array": "cpp",
63+
"atomic": "cpp",
64+
"bitset": "cpp",
65+
"cctype": "cpp",
66+
"cfenv": "cpp",
67+
"chrono": "cpp",
68+
"cinttypes": "cpp",
69+
"clocale": "cpp",
70+
"cmath": "cpp",
71+
"codecvt": "cpp",
72+
"complex": "cpp",
73+
"condition_variable": "cpp",
74+
"csetjmp": "cpp",
75+
"csignal": "cpp",
76+
"cstdarg": "cpp",
77+
"cstddef": "cpp",
78+
"cstdint": "cpp",
79+
"cstdio": "cpp",
80+
"cstdlib": "cpp",
81+
"cstring": "cpp",
82+
"ctime": "cpp",
83+
"cuchar": "cpp",
84+
"cwchar": "cpp",
85+
"cwctype": "cpp",
86+
"deque": "cpp",
87+
"forward_list": "cpp",
88+
"list": "cpp",
89+
"unordered_map": "cpp",
90+
"unordered_set": "cpp",
91+
"vector": "cpp",
92+
"exception": "cpp",
93+
"algorithm": "cpp",
94+
"functional": "cpp",
95+
"iterator": "cpp",
96+
"map": "cpp",
97+
"memory": "cpp",
98+
"memory_resource": "cpp",
99+
"numeric": "cpp",
100+
"random": "cpp",
101+
"ratio": "cpp",
102+
"regex": "cpp",
103+
"set": "cpp",
104+
"system_error": "cpp",
105+
"tuple": "cpp",
106+
"type_traits": "cpp",
107+
"utility": "cpp",
108+
"fstream": "cpp",
109+
"future": "cpp",
110+
"initializer_list": "cpp",
111+
"iomanip": "cpp",
112+
"iosfwd": "cpp",
113+
"istream": "cpp",
114+
"limits": "cpp",
115+
"mutex": "cpp",
116+
"new": "cpp",
117+
"ostream": "cpp",
118+
"scoped_allocator": "cpp",
119+
"shared_mutex": "cpp",
120+
"sstream": "cpp",
121+
"stdexcept": "cpp",
122+
"streambuf": "cpp",
123+
"thread": "cpp",
124+
"typeindex": "cpp",
125+
"typeinfo": "cpp",
126+
"valarray": "cpp"
62127
}
63128
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Algorithm: Height of Binary Tree
3+
* Description: Recursively finds the maximum number of edges on the path from the root to any leaf node.
4+
* Time Complexity: O(n)
5+
* Space Complexity: O(h) // Where 'h' is the height of the tree, due to recursion stack
6+
* Author: Adi-3108
7+
*/
8+
9+
#include <iostream>
10+
#include <algorithm> // Required for max
11+
12+
using namespace std;
13+
// 1. Structure Definition
14+
struct TreeNode {
15+
int val;
16+
TreeNode *left;
17+
TreeNode *right;
18+
19+
// Constructor
20+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
21+
};
22+
23+
/**
24+
* @brief Recursively calculates the height of the binary tree.
25+
* * Height is defined as the number of edges on the longest path from the root to a leaf.
26+
* * @param root The root of the binary tree (or subtree).
27+
* @return The height of the tree (number of edges).
28+
*/
29+
int calculateHeight(TreeNode* root) {
30+
// Base case: An empty tree (NULL) has a height of -1 (so a single node returns 0).
31+
if (root == NULL) {
32+
return -1;
33+
}
34+
35+
// Recursively find the height of the left and right subtrees
36+
int left_height = calculateHeight(root->left);
37+
int right_height = calculateHeight(root->right);
38+
39+
// The height of the current tree is 1 (for the current node/edge)
40+
// plus the maximum height of the subtrees.
41+
return 1 + max(left_height, right_height);
42+
}
43+
44+
// Test function with modern C++ features
45+
void testCalculateHeight() {
46+
// Test Case 1: Skewed Tree (Height = 3)
47+
// 1 -> 2 -> 3 -> 4
48+
TreeNode *root1 = new TreeNode(1);
49+
root1->left = new TreeNode(2);
50+
root1->left->left = new TreeNode(3);
51+
root1->left->left->left = new TreeNode(4);
52+
// for verification We are Printing the expected height
53+
cout << "Test Case 1 (Skewed Tree):\n";
54+
cout << "Expected Height: 3, Actual Height: " << calculateHeight(root1) << endl;
55+
56+
// Test Case 2: Balanced Tree (Height = 2)
57+
// 1
58+
// / \
59+
// 2 3
60+
// / \
61+
// 4 5
62+
TreeNode *root2 = new TreeNode(1);
63+
root2->left = new TreeNode(2);
64+
root2->right = new TreeNode(3);
65+
root2->left->left = new TreeNode(4);
66+
root2->left->right = new TreeNode(5);
67+
68+
cout << "\nTest Case 2 (Balanced Tree):\n";
69+
70+
// for verification We are Printing the expected height
71+
cout << "Expected Height: 2, Actual Height: " << calculateHeight(root2) << endl;
72+
73+
// Clean up memory
74+
delete root1->left->left->left;
75+
delete root1->left->left;
76+
delete root1->left;
77+
delete root1;
78+
79+
delete root2->left->left;
80+
delete root2->left->right;
81+
delete root2->left;
82+
delete root2->right;
83+
delete root2;
84+
}
85+
86+
int main() {
87+
testCalculateHeight();
88+
return 0;
89+
}

0 commit comments

Comments
 (0)