You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Your solution is correct and efficient. Well done! Here are a few minor suggestions:
Consider using a local variable and passing it via a nested function or using a mutable object (like a list) to avoid using self for the index. This makes the function more self-contained and avoids potential issues if the same Solution instance is reused (though the problem doesn't specify that).
For example:
defbuildTree(self, inorder, postorder):
idx_map= {val: ifori, valinenumerate(inorder)}
post_idx= [len(postorder)-1] # use a list to be mutabledefhelper(left, right):
ifleft>right:
returnNoneroot_val=postorder[post_idx[0]]
post_idx[0] -=1root=TreeNode(root_val)
mid=idx_map[root_val]
root.right=helper(mid+1, right)
root.left=helper(left, mid-1)
returnrootreturnhelper(0, len(inorder)-1)
Alternatively, you can define the helper function without using self by using nonlocal (if using Python 3) but that requires defining the variable in the outer function. However, your current approach is acceptable.
The code for leetcode_129.py is also correct, but note that we are only evaluating leetcode_106.py for this problem.
Overall, your solution is excellent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
leetcode_106:
Used post order traversal to identify the root node (last element).
Also inorder traversal to split the tree into left and right subtrees.
Applied DFS recursion with index boundaries to build the tree.
Constructed the right subtree before the left due to postorder order.
At last Used a hash map for fast lookup of root positions in inorder array.
Leetcode_129:
Used DFS traversal to explore all root-to-leaf paths.
Built numbers incrementally using current * 10 + node.val.
Detected leaf nodes to finalize and add path values.
Returned sums from left and right subtrees recursively.
Accumulated and returned the total sum of all root-to-leaf numbers