0
0
DSA C++programming~20 mins

Diameter of Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
šŸŽ–ļø
Diameter Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā“ Predict Output
intermediate
2:00remaining
Output of diameter calculation for a simple binary tree
What is the output of the following C++ code that calculates the diameter of a binary tree?
DSA C++
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

int maxDiameter = 0;

int depth(TreeNode* node) {
    if (!node) return 0;
    int leftDepth = depth(node->left);
    int rightDepth = depth(node->right);
    maxDiameter = std::max(maxDiameter, leftDepth + rightDepth);
    return 1 + std::max(leftDepth, rightDepth);
}

int diameterOfBinaryTree(TreeNode* root) {
    maxDiameter = 0;
    depth(root);
    return maxDiameter;
}

int main() {
    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);
    int result = diameterOfBinaryTree(root);
    std::cout << result << std::endl;
    return 0;
}
A4
B2
C3
D5
Attempts:
2 left
šŸ’” Hint
The diameter is the longest path between any two nodes, which may pass through the root.
ā“ Predict Output
intermediate
2:00remaining
Diameter output for a skewed binary tree
What will be the output of the diameterOfBinaryTree function for the following skewed binary tree?
DSA C++
TreeNode* root = new TreeNode(1);
root->right = new TreeNode(2);
root->right->right = new TreeNode(3);
root->right->right->right = new TreeNode(4);
int result = diameterOfBinaryTree(root);
std::cout << result << std::endl;
A0
B4
C1
D3
Attempts:
2 left
šŸ’” Hint
In a skewed tree, the diameter is the length of the longest path which is the number of edges in the chain.
šŸ”§ Debug
advanced
2:00remaining
Identify the error in diameter calculation code
What error will the following code produce when calculating the diameter of a binary tree?
DSA C++
int maxDiameter = 0;

int depth(TreeNode* node) {
    if (node == nullptr) return 0;
    int leftDepth = depth(node->left);
    int rightDepth = depth(node->right);
    maxDiameter = std::max(maxDiameter, leftDepth * rightDepth);
    return 1 + std::max(leftDepth, rightDepth);
}

int diameterOfBinaryTree(TreeNode* root) {
    maxDiameter = 0;
    depth(root);
    return maxDiameter;
}
AThe diameter will be incorrect because it uses multiplication instead of addition
BCompilation error due to missing return statement
CRuntime error due to null pointer dereference
DNo error, code works correctly
Attempts:
2 left
šŸ’” Hint
Check how diameter is calculated from left and right depths.
🧠 Conceptual
advanced
1:30remaining
Understanding diameter definition in binary trees
Which of the following best describes the diameter of a binary tree?
AThe number of edges on the longest path between any two nodes in the tree
BThe height of the tree plus one
CThe maximum depth of the left subtree
DThe number of nodes on the longest path between any two nodes in the tree
Attempts:
2 left
šŸ’” Hint
Diameter counts edges, not nodes.
šŸš€ Application
expert
2:30remaining
Calculate diameter after modifying the tree
Given the initial tree below, what is the diameter after adding a new node as the left child of node 5? Initial tree: 1 ā”œā”€ 2 │ ā”œā”€ 4 │ └─ 5 └─ 3 Add node 6 as left child of node 5. What is the new diameter?
DSA C++
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);
root->left->right->left = new TreeNode(6);
int result = diameterOfBinaryTree(root);
std::cout << result << std::endl;
A5
B4
C3
D6
Attempts:
2 left
šŸ’” Hint
Consider the longest path after adding the new node 6.