Skip to content

Commit 13f7aab

Browse files
Merge pull request #236 from hk27xing/hk27xing-add
添加105和113题的Java代码
2 parents 7c7f438 + 1f35002 commit 13f7aab

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

problems/0106.从中序与后序遍历序列构造二叉树.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,10 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。
580580
581581
## 其他语言版本
582582
583-
584583
Java:
584+
585+
106.从中序与后序遍历序列构造二叉树
586+
585587
```java
586588
class Solution {
587589
public TreeNode buildTree(int[] inorder, int[] postorder) {
@@ -617,8 +619,43 @@ class Solution {
617619
}
618620
```
619621

622+
105.从前序与中序遍历序列构造二叉树
623+
624+
```java
625+
class Solution {
626+
public TreeNode buildTree(int[] preorder, int[] inorder) {
627+
return helper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
628+
}
629+
630+
public TreeNode helper(int[] preorder, int preLeft, int preRight,
631+
int[] inorder, int inLeft, int inRight) {
632+
// 递归终止条件
633+
if (inLeft > inRight || preLeft > preRight) return null;
634+
635+
// val 为前序遍历第一个的值,也即是根节点的值
636+
// idx 为根据根节点的值来找中序遍历的下标
637+
int idx = inLeft, val = preorder[preLeft];
638+
TreeNode root = new TreeNode(val);
639+
for (int i = inLeft; i <= inRight; i++) {
640+
if (inorder[i] == val) {
641+
idx = i;
642+
break;
643+
}
644+
}
645+
646+
// 根据 idx 来递归找左右子树
647+
root.left = helper(preorder, preLeft + 1, preLeft + (idx - inLeft),
648+
inorder, inLeft, idx - 1);
649+
root.right = helper(preorder, preLeft + (idx - inLeft) + 1, preRight,
650+
inorder, idx + 1, inRight);
651+
return root;
652+
}
653+
}
654+
```
655+
620656
Python:
621657
105.从前序与中序遍历序列构造二叉树
658+
622659
```python
623660
# Definition for a binary tree node.
624661
# class TreeNode:
@@ -637,6 +674,7 @@ class Solution:
637674
return root
638675
```
639676
106.从中序与后序遍历序列构造二叉树
677+
640678
```python
641679
# Definition for a binary tree node.
642680
# class TreeNode:

problems/0112.路径总和.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,42 @@ class Solution {
347347
}
348348
```
349349

350+
0113.路径总和-ii
351+
352+
```java
353+
class Solution {
354+
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
355+
List<List<Integer>> res = new ArrayList<>();
356+
if (root == null) return res; // 非空判断
357+
358+
List<Integer> path = new LinkedList<>();
359+
preorderDFS(root, targetSum, res, path);
360+
return res;
361+
}
362+
363+
public void preorderDFS(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
364+
path.add(root.val);
365+
// 遇到了叶子节点
366+
if (root.left == null && root.right == null) {
367+
// 找到了和为 targetSum 的路径
368+
if (targetSum - root.val == 0) {
369+
res.add(new ArrayList<>(path));
370+
}
371+
return; // 如果和不为 targetSum,返回
372+
}
373+
374+
if (root.left != null) {
375+
preorderDFS(root.left, targetSum - root.val, res, path);
376+
path.remove(path.size() - 1); // 回溯
377+
}
378+
if (root.right != null) {
379+
preorderDFS(root.right, targetSum - root.val, res, path);
380+
path.remove(path.size() - 1); // 回溯
381+
}
382+
}
383+
}
384+
```
385+
350386
Python:
351387

352388
0112.路径总和

0 commit comments

Comments
 (0)