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;.
In a certain code language,
‘M & N’ means ‘M is the wife of N’,
‘M @ N’ means ‘M is the brother of N’,
‘M $ N’ ...
In a certain code language, ‘WING’ is coded as ‘2618’ and ‘NEAT’ is coded as ‘3469’. What is the code for ‘N’ in that language?
...Eight people E, F, G, H, J, K, L and M are sitting around a circular table facing the centre but not necessarily in the same order. F is sitting second ...
Three of the following four figure pairs are alike in a certain way and thus form a group. Which is the oue that does not belong to that group?
<...
Which two signs should be interchanged to make the given equation correct?
756 ÷ 252 + 89 × 180 – 63 = 384
Which of the following interchange of numbers (not digits) would make the given equation correct?
14 × 4 ÷ 10 + 10 – 20 = 2
Select the correct combination of mathematical signs that can sequentially replace the * signs and balance the given equation.
36 * 12 * 6 * 12 * 3 = 63
Statements:
All buses are cars.
No jeep is a car.
Conclusions:
I. No car is a jeep.
II. No jeep is a bus.
What should come in place of? in the given series based on the English alphabetical order?
UDM, XGP, AJS, DMV, ?
If a mirror is placed on the shaded line, which of the following options is the correct image of the given shape?