Skip to content

Commit 347a5c8

Browse files
committed
note
1 parent 88381dd commit 347a5c8

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

guangxu/202209/20220920.md

+57-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,66 @@
11
## Algorithm
22

3+
[106. Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)
4+
35
### Description
46

5-
### Solution
7+
Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.
8+
9+
Example 1:
10+
11+
![](assets/20220920-e354285b.png)
12+
13+
```
14+
Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
15+
Output: [3,9,20,null,null,15,7]
16+
```
617

7-
```java
18+
Example 2:
19+
20+
```
21+
Input: inorder = [-1], postorder = [-1]
22+
Output: [-1]
23+
```
24+
25+
Constraints:
26+
27+
- 1 <= inorder.length <= 3000
28+
- postorder.length == inorder.length
29+
- -3000 <= inorder[i], postorder[i] <= 3000
30+
- inorder and postorder consist of unique values.
31+
- Each value of postorder also appears in inorder.
32+
- inorder is guaranteed to be the inorder traversal of the tree.
33+
- postorder is guaranteed to be the postorder traversal of the tree.
34+
35+
### Solution
836

37+
```java
38+
/**
39+
* Definition for a binary tree node.
40+
* public class TreeNode {
41+
* int val;
42+
* TreeNode left;
43+
* TreeNode right;
44+
* TreeNode(int x) { val = x; }
45+
* }
46+
*/
47+
class Solution {
48+
public TreeNode buildTree(int[] inorder, int[] postorder) {
49+
if(inorder==null||postorder==null||inorder.length==0||postorder.length==0){
50+
return null;
51+
}
52+
TreeNode treeNode = new TreeNode(postorder[postorder.length-1]);
53+
for(int i=0;i<inorder.length;i++){
54+
if(inorder[i]==postorder[postorder.length-1]){
55+
treeNode.left = buildTree(Arrays.copyOfRange(inorder, 0, i),
56+
Arrays.copyOfRange(postorder, 0, i));
57+
treeNode.right = buildTree(Arrays.copyOfRange(inorder, i+1, inorder.length),
58+
Arrays.copyOfRange(postorder, i, postorder.length-1));
59+
}
60+
}
61+
return treeNode;
62+
}
63+
}
964
```
1065

1166
### Discuss
28.3 KB
Loading

guangxu/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| 标题 | Algorithm | Review | Tip | Share|
22
| - | - | - | - | - |
3+
| [20220920](./202209/20220920.md) |[106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)||||
34
| [20220919](./202209/20220919.md) |[105. Construct Binary Tree from Preorder and Inorder Traversal(根据前序和中序构建二叉树)](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)||||
45
| [20220918](./202209/20220918.md) |[116. Populating Next Right Pointers in Each Node(完美二叉树的每个节点增加Next指针)](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)||||
56
| [20220917](./202209/20220917.md) |[117. Populating Next Right Pointers in Each Node II(完美二叉树的每个节点增加Next指针2)](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)||||

0 commit comments

Comments
 (0)