Skip to content

Commit 452848c

Browse files
committed
树的算法。递归与二叉
1 parent f43326f commit 452848c

25 files changed

+197
-67
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package 数据结构..BST;
2+
3+
import 数据结构..TreeNode;
4+
5+
/**
6+
* Created by 周杰伦 on 2018/4/20.
7+
*/
8+
public class 从有序数组中构造二叉查找树 {
9+
// Leetcode : 108. Convert Sorted Array to Binary Search Tree (Easy)
10+
11+
public TreeNode sortedArrayToBST(int[] nums) {
12+
return toBST(nums, 0, nums.length - 1);
13+
}
14+
15+
//根据节点的位置关系进行递归
16+
private TreeNode toBST(int[] nums, int sIdx, int eIdx){
17+
if(sIdx > eIdx) return null;
18+
int mIdx = (sIdx + eIdx) / 2;
19+
TreeNode root = new TreeNode(nums[mIdx]);
20+
root.left = toBST(nums, sIdx, mIdx - 1);
21+
root.right = toBST(nums, mIdx + 1, eIdx);
22+
return root;
23+
}
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package 数据结构..BST;
2+
3+
import 数据结构..TreeNode;
4+
5+
/**
6+
* Created by 周杰伦 on 2018/4/20.
7+
*/
8+
public class 修剪二叉查找树 {
9+
// 二叉查找树(BST):根节点大于等于左子树所有节点,小于等于右子树所有节点。
10+
//
11+
// 只保留值在 L ~ R 之间的节点
12+
13+
//这个解法绝了。
14+
public TreeNode trimBST(TreeNode root, int L, int R) {
15+
if(root == null) return null;
16+
//若根节点比下限小。只保留右边。
17+
//若根节点比上限大,只保留左边。
18+
if(root.val > R) return trimBST(root.left, L, R);
19+
if(root.val < L) return trimBST(root.right, L, R);
20+
//如果根节点在中间,那么左子树也进行以上操作进行保留
21+
root.left = trimBST(root.left, L, R);
22+
root.right = trimBST(root.right, L, R);
23+
//
24+
return root;
25+
}
26+
// public TreeNode trimBST(TreeNode root, int L, int R) {
27+
// if (root == null)return null;
28+
// TreeNode t = new TreeNode(root.val);
29+
// trimBST(root.left,L,R);
30+
// if (root.val < L) {
31+
// root = root.right;
32+
//
33+
// }else if (root.val > R) {
34+
// root = root.left;
35+
// }
36+
// trimBST(root.right,L,R);
37+
// return root;
38+
// }
39+
}

src/数据结构/树/TreeNode.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* Created by 周杰伦 on 2018/4/19.
55
*/
66
public class TreeNode {
7-
int val;
8-
TreeNode left;
9-
TreeNode right;
10-
TreeNode(int x) { val = x; }
7+
public int val;
8+
public TreeNode left;
9+
public TreeNode right;
10+
public TreeNode(int x) { val = x; }
1111
}

src/数据结构/树/两节点的最长路径.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/数据结构/树/从有序数组中构造二叉查找树.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/数据结构/树/修剪二叉查找树.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/数据结构/树/子树.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/数据结构/树/最小路径.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/数据结构/树/统计左叶子节点的和.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/数据结构/树/平衡树.java renamed to src/数据结构/树/递归/平衡树.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package 数据结构.;
1+
package 数据结构..递归;
2+
3+
import 数据结构..TreeNode;
24

35
/**
46
* Created by 周杰伦 on 2018/4/20.

0 commit comments

Comments
 (0)