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;.
What is the significance of the integrated steel plant project in Gadchiroli, Maharashtra?Â
- According to UBS, what is India's projected semiconductor end-demand in 2030?
What is the projected size of India’s mining equipment market by 2033?Â
What pioneering digital communication initiative was launched by Assam using AI?
What system is used to continuously clear cheques within hours instead of the traditional two-day period?
What is the wage ceiling for the Productivity Linked Reward (PLR) bonus calculation as per the modified scheme for Major Port Authorities and Dock Labou...
What is the key objective of the RBI’s newly launched Unified Markets Interface (UMI)?Â
How much funding was announced by Union Minister Pralhad Joshi for biomass-based hydrogen pilot projects under the National Green Hydrogen Mission?Â
- What was the focus area of the Bandhan 2.0 platform launched by Axis Max Life Insurance?
Which award will RRR star Ram Charan receive at the Indian Film Festival of Melbourne (IFFM) 2024?