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?Solution
• Dry Run: o Consider a node N that has a left child (N.left is not null) but no right child (N.right is null). This is not a leaf node by definition. o Call isLeaf(N):  node == null is false.  The return statement evaluates N.left == null || N.right == null.  This becomes false || true, which evaluates to true. o So, the method returns true for a node that is not a leaf. • Why Correct Answer (C): true, which is incorrect because it's not a leaf. o The dry run confirms that the method will return true. o The explanation correctly states that this is incorrect because a node with only one child is not a leaf. A correct isLeaf implementation would use && (AND): return node.left == null && node.right == null;.
Which of the following word is correctly spelled?
Where did NMDC Limited and MECON Limited inaugurate their international offices to expand India’s industrial presence?
Which one of the following statements about ancient Indian pottery is correct?Â
In which state/UT is the Hemis festival celebrated?
Who is the main operator of the Rural Infra Development Fund in India?
Kerala is famous in what form -
The poses in which classical dance form are called Karanas?
In which of the following states of India is Padayani a ritual dance art form performed at Bhadrakali temple?
Where was the world’s first air taxi service unveiled?
Which schemes are included under Tier 1 and Tier 2 of the Payment Infrastructure Development Fund (PIDF)?