Question

A Java method isLeaf(TreeNode node) is intended to check if a given node is a leaf in a binary tree. class TreeNode {     int val;     TreeNode left, right;     TreeNode(int
x) { val = x; } } public boolean isLeaf(TreeNode node) {     if (node == null) {         return false;     }     return node.left == null || node.right == null; // Potential bug here } If isLeaf is called on a node that has a left child but no right child, what will the method return?

A true, which is correct for a leaf node.
B false, which is incorrect for a leaf node.
C true, which is incorrect because it's not a leaf.
D false, which is correct because it's not a leaf.
E A NullPointerException.
Practice Next

Hey! Ask a query