Skip to content

Commit 3c4ac9b

Browse files
committed
889
1 parent 5f691e8 commit 3c4ac9b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Tree/889.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## Construct Binary Tree from Preorder and Postorder Traversal
2+
3+
#### Description
4+
5+
[link](https://leetcode.com/problems/leaf-similar-trees/)
6+
7+
---
8+
9+
#### Solution
10+
11+
- See Code
12+
13+
---
14+
15+
#### Code
16+
17+
> Complexity T : O(N) M : O(n)
18+
19+
```python
20+
# Definition for a binary tree node.
21+
# class TreeNode:
22+
# def __init__(self, val=0, left=None, right=None):
23+
# self.val = val
24+
# self.left = left
25+
# self.right = right
26+
class Solution:
27+
def constructFromPrePost(self, pre: List[int], post: List[int]) -> TreeNode:
28+
def buid_tree(pre, post):
29+
if not pre:
30+
return None
31+
node = TreeNode(pre[0])
32+
if len(pre) < 2:
33+
return node
34+
index = post.index(pre[1])
35+
node.left = buid_tree(pre[1:2+index], post[:index+1])
36+
node.right = buid_tree(pre[2+index:], post[index+1:])
37+
return node
38+
return buid_tree(pre, post)
39+
```

0 commit comments

Comments
 (0)