File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments