Skip to content

Commit 99dffd5

Browse files
committed
树的算法。
1 parent 452848c commit 99dffd5

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
package 数据结构..递归.节点;
22

3+
import 数据结构..TreeNode;
4+
35
/**
46
* Created by 周杰伦 on 2018/4/20.
57
*/
68
public class 二叉查找树的最近公共祖先 {
9+
//根据查找树的性质,如果节点值在这两个值中间,就是最近公共祖先。
10+
//若比两个节点都大,找左子树,否则找右子树。
11+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
12+
if (root == null)return null;
13+
if (root.val >= p.val && root.val <= q.val || root.val <= p.val && root.val >= q.val ) {
14+
return root;
15+
}else if (root.val > p.val && root.val > q.val){
16+
return lowestCommonAncestor(root.left, p, q);
17+
}else if (root.val < p.val && root.val < q.val) {
18+
return lowestCommonAncestor(root.right, p, q);
19+
}
20+
return null;
21+
}
722
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,135 @@
11
package 数据结构..递归.节点;
22

3+
import 数据结构..TreeNode;
4+
5+
import java.util.ArrayList;
6+
37
/**
48
* Created by 周杰伦 on 2018/4/20.
59
*/
610
public class 二叉树的最近公共祖先 {
11+
// public static void main(String[] args) {
12+
//// TreeNode t0 = new TreeNode(3);
13+
//// TreeNode t1 = new TreeNode(5);
14+
//// TreeNode t3 = new TreeNode(6);
15+
//// TreeNode t4 = new TreeNode(2);
16+
//// TreeNode t5 = new TreeNode(7);
17+
//// TreeNode t6 = new TreeNode(4);
18+
////
19+
//// t4.left = t5;
20+
//// t4.right = t6;
21+
//// t1.left = t3;
22+
//// t1.right = t4;
23+
//// t0.left = t1;
24+
////
25+
////
26+
//// TreeNode t7 = new TreeNode(1);
27+
//// TreeNode t8 = new TreeNode(0);
28+
//// TreeNode t9 = new TreeNode(8);
29+
////
30+
//// t7.left = t8;
31+
//// t7.right = t9;
32+
//// t0.right = t7;
33+
////
34+
//// System.out.println(lowestCommonAncestor(t0, t9, t8));
35+
// TreeNode t1 = new TreeNode(1);
36+
// TreeNode t2 = new TreeNode(2);
37+
// TreeNode t3 = new TreeNode(3);
38+
// t1.left = t2;
39+
// t1.right = t3;
40+
// System.out.println(lowestCommonAncestor(t1, t2, t3));
41+
//
42+
// }
43+
44+
45+
//复杂度是n + n + n*n*2 = n2;
46+
47+
//最优解
48+
// public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
49+
// if (root == null || root == p || root == q) return root;
50+
// TreeNode left = lowestCommonAncestor(root.left, p, q);
51+
// TreeNode right = lowestCommonAncestor(root.right, p, q);
52+
// return left == null ? right : right == null ? left : root;
53+
// }
54+
55+
56+
//我的解法
57+
//先打印中序遍历
58+
//只保留两个节点中间的节点。
59+
//计算距离的最小值,值最小的是最近的。
60+
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
61+
if (root == null)return null;
62+
if (p == q)return p;
63+
if (p == root || q == root)return root;
64+
if (isParent(p,q))return p;
65+
if (isParent(q,p))return q;
66+
ArrayList<TreeNode> list = new ArrayList<>();
67+
inorder(root, list);
68+
int i = -1,j = -1;
69+
for (TreeNode t : list) {
70+
if (t == p)i = list.indexOf(p);
71+
if (t == q)j = list.indexOf(q);
72+
if (i != -1 && j != -1)break;
73+
}
74+
int left = i > j ? j : i;
75+
int right = i > j ? i : j;
76+
for (int n = right;n < list.size(); n ++) {
77+
list.remove(n);
78+
}
79+
for (int m = 0;m <= left;m ++) {
80+
list.remove(0);
81+
}
82+
TreeNode minNode = root;
83+
int min = Integer.MAX_VALUE;
84+
if (list.size() == 1)return list.get(0);
85+
for (TreeNode t : list) {
86+
if (isParent(t, p) && isParent(t, q)) {
87+
int cnt = cnt(t, p, 1) + cnt(t, q, 1);
88+
if (cnt < min) {
89+
min = cnt;
90+
minNode = t;
91+
}
92+
}
93+
}
94+
return minNode;
95+
}
96+
public static void inorder(TreeNode root, ArrayList<TreeNode> list) {
97+
if (root == null)return;
98+
inorder(root.left, list);
99+
list.add(root);
100+
inorder(root.right, list);
101+
return;
102+
}
103+
public static int cnt (TreeNode root, TreeNode p, int cnt) {
104+
if (root == null)return 0;
105+
if (root == p)return cnt;
106+
int left = cnt(root.left, p, cnt + 1);
107+
int right = cnt(root.right, p, cnt +1);
108+
return left != 0 ? left : right;
109+
}
110+
public static boolean isParent(TreeNode t, TreeNode p) {
111+
if (t == p) return true;
112+
if (t == null) return false;
113+
return isParent(t.left, p) || isParent(t.right, p);
114+
}
115+
//
116+
// public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
117+
// if (root == null) return null;
118+
// if (isParent(root, q)) return p;
119+
// if (isParent(q, p)) return q;
120+
// return inorder(root, p, q, root);
121+
// }
122+
//
123+
// public TreeNode inorder(TreeNode root, TreeNode p, TreeNode q, TreeNode pre) {
124+
// if (root == null) return pre;
125+
// if (isParent(root, p) && isParent(root, q)) {
126+
// TreeNode left = inorder(root.left, p, q, root);
127+
// TreeNode right = inorder(root.right, p, q, root);
128+
// if (left != null) return left;
129+
// if (right != null) return right;
130+
// }
131+
// return null;
132+
// }
133+
7134
}
135+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
package 数据结构..递归.节点;
22

3+
import 数据结构..TreeNode;
4+
35
/**
46
* Created by 周杰伦 on 2018/4/20.
57
*/
68
public class 找出二叉树中第二小的节点 {
9+
public static void main(String[] args) {
10+
}
11+
//先找到最小值,再找第二小值
12+
public int findSecondMinimumValue(TreeNode root) {
13+
if (root == null)return -1;
14+
int first = findMin(root, Integer.MAX_VALUE, -1);
15+
int second = findMin(root, Integer.MAX_VALUE, first);
16+
if (second == Integer.MAX_VALUE)return - 1;
17+
return second;
18+
}
19+
public int findMin (TreeNode root, int min, int f) {
20+
if (root == null)return min;
21+
if (root.val < min && root.val != f)min = root.val;
22+
min = Math.min(findMin(root.left, min, f), min);
23+
min = Math.min(findMin(root.right, min, f), min);
24+
return min;
25+
}
26+
727
}

0 commit comments

Comments
 (0)