Skip to content

Commit 613e0ef

Browse files
committed
Sync LeetCode submission Runtime - 7 ms (9.15%), Memory - 17.5 MB (100.00%)
1 parent d81f21e commit 613e0ef

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p>Given two integer arrays, <code>preorder</code> and <code>postorder</code> where <code>preorder</code> is the preorder traversal of a binary tree of <strong>distinct</strong> values and <code>postorder</code> is the postorder traversal of the same tree, reconstruct and return <em>the binary tree</em>.</p>
2+
3+
<p>If there exist multiple answers, you can <strong>return any</strong> of them.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/24/lc-prepost.jpg" style="width: 304px; height: 265px;" />
8+
<pre>
9+
<strong>Input:</strong> preorder = [1,2,4,5,3,6,7], postorder = [4,5,2,6,7,3,1]
10+
<strong>Output:</strong> [1,2,3,4,5,6,7]
11+
</pre>
12+
13+
<p><strong class="example">Example 2:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> preorder = [1], postorder = [1]
17+
<strong>Output:</strong> [1]
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Constraints:</strong></p>
22+
23+
<ul>
24+
<li><code>1 &lt;= preorder.length &lt;= 30</code></li>
25+
<li><code>1 &lt;= preorder[i] &lt;= preorder.length</code></li>
26+
<li>All the values of <code>preorder</code> are <strong>unique</strong>.</li>
27+
<li><code>postorder.length == preorder.length</code></li>
28+
<li><code>1 &lt;= postorder[i] &lt;= postorder.length</code></li>
29+
<li>All the values of <code>postorder</code> are <strong>unique</strong>.</li>
30+
<li>It is guaranteed that <code>preorder</code> and <code>postorder</code> are the preorder traversal and postorder traversal of the same binary tree.</li>
31+
</ul>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Approach: Recursion
2+
3+
# Time: O(n^2)
4+
# Space: O(n)
5+
6+
# Definition for a binary tree node.
7+
# class TreeNode:
8+
# def __init__(self, val=0, left=None, right=None):
9+
# self.val = val
10+
# self.left = left
11+
# self.right = right
12+
class Solution:
13+
def constructFromPrePost(self, preorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
14+
if not preorder or not postorder:
15+
return None
16+
17+
# Create root node from first element of preorder
18+
root = TreeNode(preorder[0])
19+
20+
if len(preorder) == 1:
21+
return root
22+
23+
# Find the left subtree root in postorder
24+
# The second element in preorder is the root of left subtree
25+
left_root_val = preorder[1]
26+
left_root_index = postorder.index(left_root_val)
27+
28+
left_size = left_root_index + 1
29+
30+
left_preorder = preorder[1 : left_size + 1]
31+
right_preorder = preorder[left_size + 1 :]
32+
33+
left_postorder = postorder[: left_size]
34+
right_postorder = postorder[left_size : -1]
35+
36+
root.left = self.constructFromPrePost(left_preorder, left_postorder)
37+
root.right = self.constructFromPrePost(right_preorder, right_postorder)
38+
39+
return root
40+

0 commit comments

Comments
 (0)