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;.
Ram is 55 years old and Som is 25 years old. How many years ago was Ram three times as old as Som?
Which rice variety of Bihar's West Champaran has been awarded the GI tag?
In a certain code language, ‘FROM’ is coded as ‘4935’ and ‘MORE’ is coded as ‘3154’. What is the code for ‘E’ in the given code lang...
Fruits and seeds have a high concentration of which of the following plant hormones?
Expenditure of installation of a new machinery, is:Â
The Trade Union Act, of 1926 provides for the dissolution of a trade union by a special resolution of its members or by an order of the _______.
Which of the following statements is/are correct in regards to Gati Shakti University?
1.It was granted central status by the Union Cabinet in 20...
Which one of the following countries is not a member of WTO?
Find out which of the answer figures from the options can be formed using all the pieces given in the problem figure.
Which of the following errors would not be detected by a trial balance?