Done Tree 2#1579
Conversation
Construct Binary Tree from Inorder and Postorder Traversal (Problem1.py)Strengths:
Areas for improvement:
Corrected code: from typing import List, Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
inOrderIndexMap = {}
for idx, val in enumerate(inorder):
inOrderIndexMap[val] = idx
self.rootIndex = len(postorder) - 1
def helper(left, right):
if left > right:
return None
rootVal = postorder[self.rootIndex]
self.rootIndex -= 1
rootIdxInorder = inOrderIndexMap[rootVal]
rootNode = TreeNode(rootVal)
rootNode.right = helper(rootIdxInorder+1, right)
rootNode.left = helper(left, rootIdxInorder-1)
return rootNode
return helper(0, len(inorder)-1)But note: using def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
inOrderIndexMap = {val: idx for idx, val in enumerate(inorder)}
rootIndex = [len(postorder)-1]
def helper(left, right):
if left > right:
return None
rootVal = postorder[rootIndex[0]]
rootIndex[0] -= 1
rootIdxInorder = inOrderIndexMap[rootVal]
root = TreeNode(rootVal)
root.right = helper(rootIdxInorder+1, right)
root.left = helper(left, rootIdxInorder-1)
return root
return helper(0, len(inorder)-1)VERDICT: NEEDS_IMPROVEMENT Root to Leaf Sum (Problem2.py)Your approach to use recursion is good, but there are several issues to address:
Let me clarify the correct approach: In your code, you are doing: Also, you are passing currentNumber*10 to the helper, which is correct for the children? But then you add the root.val in the children? This is inconsistent. Actually, in your helper, you are receiving currentNumber which is already multiplied by 10 by the parent. Then you add the root.val. But this means that for the root, the initial call passes 0*10=0, then adds root.val -> correct. For a child, the parent passes (currentNumber * 10) which is correct, but then the child adds its value without multiplying? So it's correct only if the parent multiplies by 10. However, the issue is that the parent's currentNumber already includes the parent's value? Wait, let's trace: For root: currentNumber = 0 (passed as 010=0) + root.val -> becomes root.val. So actually, the way you are updating currentNumber in the helper is correct: first the parent multiplies by 10 and passes, then the child adds its value. But note: in the helper, you are not using the passed currentNumber correctly? The parameter currentNumber in the helper is actually the value passed from the parent (which is the parent's currentNumber * 10). Then you do: But wait: the initial call is So the logic for building the number is correct. However, the handling of result is problematic. The main issue is that you are passing result as a parameter and returning it. This can work if you are careful, but in your code, you are doing: if root.left: result = self.helper(root.left, currentNumber10, result) But note: when you call for left and right, you are passing the same result value? And then you update result from the left call and use that updated result for the right call? This is correct for accumulation. However, after processing children, you check if it's a leaf and then add currentNumber to result. But wait: if it is a leaf, you add currentNumber to result and return. But if it's not a leaf, you don't add anything? Actually, you should only add at leaves. So the code does: But for a leaf: it adds currentNumber to result and returns. So the leaf returns the updated result. So the accumulation might be correct. However, there is a problem: the initial call passes result=0 to the helper. But the helper returns the result. So the top-level call should be: return self.helper(root, 0, 0) But in your code, you are doing: Here, currentNumber is 0 at start, so it passes (0*10=0) and result=0. So it's equivalent to self.helper(root,0,0). Now, let's test with a simple tree: root=TreeNode(1), and no children. In the helper: currentNumber = 0 + 1 = 1. So it returns 1. Correct. Now with root=1, left=2. Now with root=1, right=3. So the code might actually be correct for these cases. But wait: what if there are two children? So the code seems to work. However, there is a subtle issue: the parameter But note: the code is inefficient because it passes result as a parameter and returns it. This is not typical and VERDICT: NEEDS_IMPROVEMENT |
No description provided.