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;.
During the year 1991–92, the industrial production recorded a ________growth of _______.
Match the following -
In the Koppen’s climatic classification, ‘AW’ represents –
The World Bank will provide _____ $ billion loan to support India’s efforts at providing social assistance to the poor and vulnerable households, seve...
Which of the following activity is a part of the Secondary sector of economic activities?
In 1957, who redefined a model that predicted the shape of individual molecules based on the extent of electron-pair electrostatic repulsion?
Which companies still account for 80% of transactions, despite the allowance for new players to scale payments services to 100 million users?
_____ are longitudinal valleys formed as a result of folding when Eurasian plates and Indian plates collided.
Which country will host the FIFA Women's World Cup in 2027?
Finance Minister Nirmala Sitharaman announced to set up “DESH-Stack e-portal”. What does ‘E’ stands for in DESH?