Skip to content

Commit a85858f

Browse files
author
Your Name
committed
深度与广搜
1 parent d97b38c commit a85858f

File tree

2 files changed

+154
-6
lines changed

2 files changed

+154
-6
lines changed

src/com/blankj/easy/_0107/Solution.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import com.blankj.structure.TreeNode;
55
import com.blankj.structure.TreeNodeMe;
66

7+
import java.util.ArrayDeque;
78
import java.util.LinkedList;
89
import java.util.List;
10+
import java.util.Queue;
911

1012
import static java.lang.System.out;
1113

@@ -53,24 +55,61 @@ public static void main2(String[] args) {
5355
// System.out.println(solution.levelOrderBottom(TreeNode.createTestData("[9,-42,-42,null,76,76,null,null,13,null,13]")));
5456
}
5557

58+
/**
59+
* 0
60+
* / \
61+
* 1 2
62+
* / \ / \
63+
* 3 4 5 6
64+
* @param args
65+
*/
5666
public static void main(String[] args) {
5767
Solution solution = new Solution();
58-
List<List<Integer>> lists = solution.levelOrderBottomMe(TreeNodeMe.createTestData("[1,2,2,3,4,4,3]"));
68+
TreeNodeMe treeNodes = TreeNodeMe.createTestData("[0,1,2,3,4,5,6]");
69+
List<List<Integer>> lists = solution.depthSearch(treeNodes);
5970
// out.println(lists);
71+
solution.levelSearch(treeNodes);
6072
}
6173

62-
public List<List<Integer>> levelOrderBottomMe(TreeNodeMe root) {
74+
public List<List<Integer>> depthSearch(TreeNodeMe root) {
6375
List<List<Integer>> list = new LinkedList<>();
64-
helperMe(root);
76+
out.print("深度搜索: ");
77+
depth(root);
78+
out.println();
6579
return list;
6680
}
6781

68-
private void helperMe(TreeNodeMe root) {
82+
/**
83+
* 这里就是深度遍历(也就是前序遍历)
84+
* @param root
85+
*/
86+
private void depth(TreeNodeMe root) {
6987
if (root == null) return;
7088
out.print(root.value + " ");
71-
helperMe(root.left);
72-
helperMe(root.right);
89+
depth(root.left);
90+
depth(root.right);
7391

7492
}
7593

94+
/**
95+
* 广度 0 1 2 3 4 5 6
96+
* @param root
97+
*/
98+
private void levelSearch(TreeNodeMe root) {
99+
if (root == null) return;
100+
Queue<TreeNodeMe> queue = new ArrayDeque();
101+
queue.add(root);
102+
out.print("广度搜索: ");
103+
while (!queue.isEmpty()) {
104+
TreeNodeMe header = queue.remove();
105+
out.print(header.value + " ");
106+
if (header.left != null) {
107+
queue.add(header.left);
108+
}
109+
if (header.right != null) {
110+
queue.add(header.right);
111+
}
112+
}
113+
}
114+
76115
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
2+
import java.util.ArrayDeque;
3+
import java.util.Stack;
4+
5+
public class TreeTest {
6+
static class TreeNode {
7+
int value;
8+
TreeNode left;
9+
TreeNode right;
10+
11+
public TreeNode(int value) {
12+
this.value = value;
13+
}
14+
}
15+
16+
TreeNode root;
17+
18+
public TreeTest(int[] array) {
19+
root = makeBinaryTreeByArray(array, 1);
20+
}
21+
22+
/**
23+
* 采用递归的方式创建一颗二叉树
24+
* 传入的是二叉树的数组表示法
25+
* 构造后是二叉树的二叉链表表示法
26+
*/
27+
public static TreeNode makeBinaryTreeByArray(int[] array, int index) {
28+
if (index < array.length) {
29+
int value = array[index];
30+
if (value != 0) {
31+
TreeNode t = new TreeNode(value);
32+
array[index] = 0;
33+
t.left = makeBinaryTreeByArray(array, index * 2);
34+
t.right = makeBinaryTreeByArray(array, index * 2 + 1);
35+
return t;
36+
}
37+
}
38+
return null;
39+
}
40+
41+
/**
42+
* 深度优先遍历,相当于先根遍历
43+
* 采用非递归实现
44+
* 需要辅助数据结构:栈
45+
*/
46+
public void depthOrderTraversal() {
47+
if (root == null) {
48+
System.out.println("empty tree");
49+
return;
50+
}
51+
// ArrayDeque<TreeNode> stack=new ArrayDeque<TreeNode>();
52+
// 0,13,65,5,97,25,0,37,22,0,4,28,0,0,32,0
53+
Stack<TreeNode> stack = new Stack(); //也可以用栈实现
54+
stack.push(root);
55+
while (stack.isEmpty() == false) {
56+
TreeNode node = stack.pop();
57+
System.out.print(node.value + " "); // 先访问自己
58+
if (node.right != null) {
59+
stack.push(node.right);
60+
}
61+
if (node.left != null) { // 最后访问左节点
62+
stack.push(node.left);
63+
}
64+
}
65+
System.out.print("\n");
66+
}
67+
68+
/**
69+
* 广度优先遍历
70+
* 采用非递归实现
71+
* 需要辅助数据结构:队列
72+
*/
73+
public void levelOrderTraversal() {
74+
if (root == null) {
75+
System.out.println("empty tree");
76+
return;
77+
}
78+
ArrayDeque<TreeNode> queue = new ArrayDeque<TreeNode>();
79+
queue.add(root);
80+
while (queue.isEmpty() == false) {
81+
TreeNode node = queue.remove();
82+
System.out.print(node.value + " ");
83+
if (node.left != null) {
84+
queue.add(node.left);
85+
}
86+
if (node.right != null) {
87+
queue.add(node.right);
88+
}
89+
}
90+
System.out.print("\n");
91+
}
92+
93+
/**
94+
* 13
95+
* / \
96+
* 65 5
97+
* / \ \
98+
* 97 25 37
99+
* / /\ /
100+
* 22 4 28 32
101+
*/
102+
public static void main(String[] args) {
103+
int[] arr = {0, 13, 65, 5, 97, 25, 0, 37, 22, 0, 4, 28, 0, 0, 32, 0};
104+
TreeTest tree = new TreeTest(arr);
105+
tree.depthOrderTraversal(); //深度优先: 深度就是前序 13 65 97 22 25 4 28 5 37 32
106+
tree.levelOrderTraversal(); //广度优先: 13 65 5 97 25 37 22 4 28 32
107+
}
108+
109+
}

0 commit comments

Comments
 (0)