Skip to content

Commit 3e308c6

Browse files
committed
最大深度、平衡二叉树、镜像二叉树
1 parent e8ac0ea commit 3e308c6

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,10 @@
7777
### 二叉树
7878

7979
- [二叉树的深度优先遍历和广度优先遍历][traversal-binary-search-tree]
80+
- [二叉树的最大深度][max-depth]
81+
- [判断是否是平衡二叉树][judge-binary-search-tree]
82+
- [判断是否是镜像二叉树][judge-binary-search-tree]
8083

8184
[traversal-binary-search-tree]: ./src/com/fantasy/algorithm/tree/TraversalBinarySearchTree.java
85+
[max-depth]: ./src/com/fantasy/algorithm/tree/MaxDepth.java
86+
[judge-binary-search-tree]: ./src/com/fantasy/algorithm/tree/JudgeBinarySearchTree.java
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.fantasy.algorithm.tree;
2+
3+
import com.fantasy.datastructure.tree.TreeNode;
4+
5+
/**
6+
* 判断二叉树的一些特性的相关算法
7+
*
8+
* <pre>
9+
* author : Fantasy
10+
* version : 1.0, 2020-08-18
11+
* since : 1.0, 2020-08-18
12+
* </pre>
13+
*/
14+
public class JudgeBinarySearchTree {
15+
16+
public static void main(String[] args) {
17+
TreeNode root = new TreeNode(1);
18+
TreeNode node2 = new TreeNode(2);
19+
TreeNode node3 = new TreeNode(2);
20+
TreeNode node4 = new TreeNode(3);
21+
TreeNode node5 = new TreeNode(4);
22+
TreeNode node6 = new TreeNode(4);
23+
TreeNode node7 = new TreeNode(3);
24+
root.left = node2;
25+
root.right = node3;
26+
node2.left = node4;
27+
node2.right = node5;
28+
node3.left = node6;
29+
node3.right = node7;
30+
31+
System.out.println("isBalanced : " + isBalanced(root));
32+
System.out.println("isSymmetric : " + isSymmetric(root));
33+
}
34+
35+
private static boolean isBalanced = true;
36+
37+
/**
38+
* 是否是平衡二叉树
39+
*/
40+
public static boolean isBalanced(TreeNode root) {
41+
isBalanced = true;
42+
getDepth(root);
43+
return isBalanced;
44+
}
45+
46+
public static int getDepth(TreeNode root) {
47+
if (root == null) {
48+
return 0;
49+
}
50+
int left = getDepth(root.left);
51+
int right = getDepth(root.right);
52+
if (Math.abs(left - right) > 1) {
53+
isBalanced = false;
54+
}
55+
return left > right ? left + 1 : right + 1;
56+
}
57+
58+
/**
59+
* 是否是镜像二叉树
60+
*/
61+
public static boolean isSymmetric(TreeNode root) {
62+
if (root == null) {
63+
return true;
64+
}
65+
return isSym(root.left, root.right);
66+
}
67+
68+
public static boolean isSym(TreeNode r1, TreeNode r2) {
69+
if (r1 == null && r2 == null) {
70+
return true;
71+
}
72+
if (r1 == null || r2 == null) {
73+
return false;
74+
}
75+
return r1.value == r2.value && isSym(r1.left, r2.right) && isSym(r1.right, r2.left);
76+
}
77+
78+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.fantasy.algorithm.tree;
2+
3+
import java.util.LinkedList;
4+
5+
import com.fantasy.datastructure.tree.BinarySearchTree;
6+
import com.fantasy.datastructure.tree.TreeNode;
7+
8+
/**
9+
* 二叉树的最大深度
10+
*
11+
* <pre>
12+
* author : Fantasy
13+
* version : 1.0, 2020-08-18
14+
* since : 1.0, 2020-08-18
15+
* </pre>
16+
*/
17+
public class MaxDepth {
18+
19+
public static void main(String[] args) {
20+
// 3
21+
// / \
22+
// 2 5
23+
// / / \
24+
// 1 4 6
25+
BinarySearchTree tree = new BinarySearchTree();
26+
tree.insert(3);
27+
tree.insert(2);
28+
tree.insert(5);
29+
tree.insert(1);
30+
tree.insert(4);
31+
tree.insert(6);
32+
33+
System.out.println("max depth : " + getMaxDepth(tree.getRoot()));
34+
System.out.println("max depth2 : " + getMaxDepth2(tree.getRoot()));
35+
}
36+
37+
public static int getMaxDepth(TreeNode root) {
38+
if (root == null) {
39+
return 0;
40+
}
41+
42+
int leftDepth = getMaxDepth(root.left);
43+
int rightDepth = getMaxDepth(root.right);
44+
45+
return leftDepth > rightDepth ? (leftDepth + 1) : (rightDepth + 1);
46+
}
47+
48+
public static int getMaxDepth2(TreeNode root) {
49+
if (root == null) {
50+
return 0;
51+
}
52+
53+
TreeNode current = null;
54+
LinkedList<TreeNode> queue = new LinkedList<>();
55+
queue.offer(root);
56+
int cur, last;
57+
int level = 0;
58+
59+
while (!queue.isEmpty()) {
60+
cur = 0; // 记录本层已经遍历的结点个数
61+
last = queue.size(); // 当遍历完当前层以后,队列里元素全是下一层的元素,队列的长度是这一层的结点的个数
62+
while (cur < last) { // 当还没有遍历到本层最后一个结点时,继续循环
63+
current = queue.poll(); // 出队一个元素
64+
cur++;
65+
// 把当前结点的左右结点入队(如果存在的话)
66+
if (current.left != null) {
67+
queue.offer(current.left);
68+
}
69+
if (current.right != null) {
70+
queue.offer(current.right);
71+
}
72+
}
73+
level++; // 每遍历完一层level+1
74+
}
75+
76+
return level;
77+
}
78+
79+
}

0 commit comments

Comments
 (0)