Consider the following binary tree structure and code to calculate its height. What will be the output?
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int height(struct Node* root) {
if (root == NULL) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
// Tree:
// 1
// / \
// 2 3
// /
// 4
int main() {
struct Node n4 = {4, NULL, NULL};
struct Node n2 = {2, &n4, NULL};
struct Node n3 = {3, NULL, NULL};
struct Node n1 = {1, &n2, &n3};
int h = height(&n1);
printf("%d\n", h);
return 0;
}Height is the number of nodes on the longest path from root to a leaf.
The longest path is 1 -> 2 -> 4, which has 3 nodes, so height is 3.
Given the following skewed binary tree and height function, what is the output?
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int height(struct Node* root) {
if (root == NULL) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
// Tree:
// 1
// \
// 2
// \
// 3
// \
// 4
int main() {
struct Node n4 = {4, NULL, NULL};
struct Node n3 = {3, NULL, &n4};
struct Node n2 = {2, NULL, &n3};
struct Node n1 = {1, NULL, &n2};
int h = height(&n1);
printf("%d\n", h);
return 0;
}Height counts nodes along the longest path from root to leaf.
The tree is skewed to the right with nodes 1 -> 2 -> 3 -> 4, so height is 4.
Analyze the following code snippet. What error will it cause when executed?
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int height(struct Node* root) {
if (root == NULL) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
int main() {
struct Node* root = NULL;
int h = height(root);
printf("%d\n", h);
return 0;
}Check the base case when root is NULL.
The base case returns 0 when root is NULL, so height of empty tree is 0, no error occurs.
Given the full binary tree below, what will the height function output?
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int height(struct Node* root) {
if (root == NULL) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
// Tree:
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
int main() {
struct Node n4 = {4, NULL, NULL};
struct Node n5 = {5, NULL, NULL};
struct Node n6 = {6, NULL, NULL};
struct Node n7 = {7, NULL, NULL};
struct Node n2 = {2, &n4, &n5};
struct Node n3 = {3, &n6, &n7};
struct Node n1 = {1, &n2, &n3};
int h = height(&n1);
printf("%d\n", h);
return 0;
}Height counts nodes along the longest path from root to leaf.
The longest path is 1 -> 2 -> 4 (or 1 -> 3 -> 6), which has 3 nodes, so height is 3.
A perfect binary tree is a full binary tree where all leaves are at the same level. Given the height is 5, how many nodes does it contain?
Number of nodes in perfect binary tree = 2^height - 1
For height 5, nodes = 2^5 - 1 = 32 - 1 = 31. But height here counts nodes, so height 5 means levels 1 to 5, total nodes = 2^5 - 1 = 31.
