Complete Trees-2#1589
Conversation
Construct Binary Tree from Inorder and Postorder Traversal (Problem45.java)Strengths:
Areas for Improvement:
Overall, the solution is excellent and matches the reference solution closely. VERDICT: PASS Root to Leaf Sum (Problem46.java)Your solution is on the right track, but there is a logical error in the placement of the leaf node check. Currently, you are checking if the current node is a leaf only after traversing the left subtree. This means that for a leaf node in the right subtree, the check will never be performed because the recursive call for the right subtree happens after the check. Additionally, for a leaf node in the left subtree, the check is performed after the left recursive call, which is unnecessary since the left recursive call will return immediately for a leaf (because its left and right are null). However, the main issue is that the right leaf nodes are never processed. To fix this, you should check for a leaf node immediately after updating the current number, before making any recursive calls. This ensures that every leaf node is correctly identified and its value added to the result. Here's the corrected version of your helper method: private void helper(TreeNode root, int currSum) {
if (root == null) return;
currSum = currSum * 10 + root.val;
if (root.left == null && root.right == null) {
result += currSum;
return; // optional: return early since no children
}
helper(root.left, currSum);
helper(root.right, currSum);
}Alternatively, you can keep the recursive calls without the early return, but the key is to move the leaf check to after updating the current sum and before the recursive calls. Another point: your code style is good, and you have correctly analyzed the time and space complexity. Keep up the good work in writing clean code! VERDICT: NEEDS_IMPROVEMENT |
No description provided.