Complete the code to create a root node for a binary tree.
TreeNode root = new TreeNode([1]);The root node value should be an integer, so 10 is correct.
Complete the code to link the left child node to the root.
root.left = new TreeNode([1]);The left child node should have a numeric value, so 5 is correct.
Fix the error in the code to add a right child node.
root.right = new TreeNode([1]);The right child node should have a numeric value, so 15 is correct.
Fill both blanks to create a method that checks if a node has children.
public boolean hasChildren(TreeNode node) {
return node.[1] != null || node.[2] != null;
}To check if a node has children, we check if left or right child is not null.
Fill all three blanks to create a method that counts nodes in a binary tree.
public int countNodes(TreeNode [1]) { if ([2] == null) return 0; return 1 + countNodes([3].left) + countNodes([3].right); }
The method uses 'node' as the parameter and variable to count nodes recursively.
