Skip to content

Commit f43326f

Browse files
committed
树的算法。开篇,主要是递归
1 parent 68186bb commit f43326f

18 files changed

+228
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package 数据结构.;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/19.
5+
*/
6+
public class TreeNode {
7+
int val;
8+
TreeNode left;
9+
TreeNode right;
10+
TreeNode(int x) { val = x; }
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package 数据结构.;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/20.
5+
*/
6+
public class 两节点的最长路径 {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package 数据结构.;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/20.
5+
*/
6+
public class 二叉查找树的最近公共祖先 {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package 数据结构.;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/20.
5+
*/
6+
public class 二叉树的最近公共祖先 {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package 数据结构.;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/20.
5+
*/
6+
public class 从有序数组中构造二叉查找树 {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package 数据结构.;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/20.
5+
*/
6+
public class 修剪二叉查找树 {
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package 数据结构.;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/20.
5+
*/
6+
public class 判断路径和是否等于一个数 {
7+
//本题有个坑。空树一定返回false。所以要判断左右子树为空并且val = sum。
8+
//而不能直接判断sum = 0 && root = null
9+
public boolean hasPathSum(TreeNode root, int sum) {
10+
if (root == null) return false;
11+
if (root.left == null && root.right == null && root.val == sum)return true;
12+
sum -= root.val;
13+
boolean left = hasPathSum(root.left, sum);
14+
boolean right = hasPathSum(root.right, sum);
15+
return left || right;
16+
}
17+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package 数据结构.;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/20.
5+
*/
6+
public class 子树 {
7+
}

src/数据结构/树/平衡树.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package 数据结构.;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/20.
5+
*/
6+
public class 平衡树 {
7+
public boolean isBalanced(TreeNode root) {
8+
if (root == null)return true;
9+
int left = maxDepth(root.left);
10+
int right = maxDepth(root.right);
11+
if (Math.abs(left - right) > 1)return false;
12+
return isBalanced(root.left) && isBalanced(root.right);
13+
}
14+
public int maxDepth(TreeNode root) {
15+
if (root == null)return 0;
16+
int left = maxDepth(root.left);
17+
int right = maxDepth(root.right);
18+
return (left > right ? left : right) + 1;
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package 数据结构.;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/20.
5+
*/
6+
public class 归并两棵树 {
7+
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
8+
if (t1 == null)return t2;
9+
if (t2 == null)return t1;
10+
TreeNode t = new TreeNode(t1.val);
11+
t.val = t1.val + t2.val;
12+
t.left = mergeTrees(t1.left, t2.left);
13+
t.right = mergeTrees(t1.right, t2.right);
14+
return t;
15+
}
16+
17+
}

0 commit comments

Comments
 (0)