Skip to content

Commit 4ae761b

Browse files
committed
添加二叉树的广度优先和深度优先遍历算法
1 parent aa5c4e6 commit 4ae761b

File tree

11 files changed

+103
-16
lines changed

11 files changed

+103
-16
lines changed
Binary file not shown.
-4 Bytes
Binary file not shown.
Binary file not shown.
776 Bytes
Binary file not shown.

bin/com/offer/JumpStep.class

1.01 KB
Binary file not shown.

bin/com/sogou/HelloSogou$1.class

4 Bytes
Binary file not shown.

bin/com/sogou/HelloSogou.class

2 Bytes
Binary file not shown.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.duwei.designpattern.singleton;
22

33
class StaticInnerSingleton {
4-
private StaticInnerSingleton() {
5-
}
4+
private StaticInnerSingleton() {}
65

76
private static class HolderClass {
8-
//静态实例属于类,所有HolderClass对象共享一个Singleton实例,保证线程安全
7+
//静态实例属于类,所有HolderClass对象共享一个Singleton实例,
8+
//保证线程安全
99
private final static StaticInnerSingleton instance = new StaticInnerSingleton();
1010
}
1111

1212
public static StaticInnerSingleton getInstance() {
13-
return HolderClass.instance; //初始化类时才初始化外部类对象:懒加载
13+
return HolderClass.instance;
14+
//初始化类时才初始化外部类对象:懒加载
1415
}
1516
}

src/com/duweri/interview/datastructure/TraversalBinaryTree.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.duweri.interview.datastructure;
22

3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
35
import java.util.Stack;
46

57
public class TraversalBinaryTree {
68

7-
public void printNode(Node node) {
9+
private void printNode(Node node) {
810
System.out.print(node.getData());
911
}
1012

@@ -103,6 +105,45 @@ public void thePostOrderTraversal_Stack(Node root) {
103105
}
104106
}
105107

108+
/**
109+
* 深度优先遍历二叉树
110+
* @param root 根节点
111+
*/
112+
public void depthFirstSearch(Node root){
113+
Stack<Node> stack = new Stack<Node>();
114+
stack.push(root);
115+
Node node = root;
116+
while(!stack.empty()){
117+
node = stack.pop();
118+
printNode(node); //遍历根结点
119+
if(node.getRightNode() != null){
120+
stack.push(node.getRightNode()); //先将右子树压栈
121+
}
122+
if(node.getLeftNode() != null){
123+
stack.push(node.getLeftNode()); //再将左子树压栈
124+
}
125+
}
126+
}
127+
/**
128+
* 广度优先遍历二叉树
129+
* @param node 根节点
130+
*/
131+
public void breadthFirst(Node node) {
132+
Deque<Node> nodeDeque = new ArrayDeque();
133+
nodeDeque.add(node);
134+
while (!nodeDeque.isEmpty()) {
135+
node = nodeDeque.peekFirst();
136+
printNode(node);
137+
if (node.getLeftNode()!=null) {
138+
nodeDeque.add(node.getLeftNode());
139+
}
140+
if (node.getRightNode()!=null) {
141+
nodeDeque.add(node.getRightNode());
142+
}
143+
}
144+
}
145+
146+
106147
public static void main(String[] args) {
107148
TraversalBinaryTree tree = new TraversalBinaryTree();
108149
Node root = tree.init();

src/com/offer/JumpStep.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.offer;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 青蛙跳台阶问题:求斐波那契数列
7+
* <有一楼梯共m级,刚开始时你在第一级,>
8+
* <若每次只能跨上一级或二级,要走上第m级,>
9+
* <共有多少走法?>
10+
* @author 杜伟
11+
*/
12+
13+
public class JumpStep {
14+
public static void main(String[] args) {
15+
Scanner sc = new Scanner(System.in);
16+
JumpStep m = new JumpStep();
17+
while(sc.hasNext()) {
18+
int num = sc.nextInt();
19+
for(int i=0; i<num; i++) {
20+
System.out.println(m.Fan(sc.nextInt()));
21+
}
22+
}
23+
24+
}
25+
public int Fan(int n) {
26+
if(n == 1) return 0;
27+
if(n == 2) return 1;
28+
if(n == 3) return 2;
29+
return Fan(n-1)+Fan(n-2);
30+
}
31+
}
32+

0 commit comments

Comments
 (0)