Complete the code to create the root node of a tree.
TreeNode root = new TreeNode([1]);The root node is usually created with a value, here we use 1 as the root's value.
Complete the code to check if a node is a leaf (no children).
boolean isLeaf = (node.left == null && node.[1] == null);A leaf node has no left or right children, so both must be null.
Fix the error in the code to calculate the height of a node.
int height = 1 + Math.max(height(node.left), [1]);
To calculate height, we need the height of the right child recursively, so use height(node.right).
Fill both blanks to calculate the depth of a node given its parent.
int depth = parent.[1] + [2];
The depth of a node is the depth of its parent plus 1.
Fill all three blanks to create a method that returns the level of a node in the tree.
public int getLevel(TreeNode node) {
if (node == null) return [1];
return getLevel(node.parent) + [2]; // level is depth + 1
}
// Usage:
int level = getLevel([3]);The base case returns 0 for null node. Each recursive call adds 1 to parent's level. The method is called with the node itself.
