Skip to content

Commit e678e71

Browse files
bjyangrubingyangrubing
authored andcommitted
commit
1 parent 770f1ed commit e678e71

File tree

16 files changed

+468
-61
lines changed

16 files changed

+468
-61
lines changed

swordForOffer/pom.xml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
44

5-
<groupId>com.netease.yangrubing</groupId>
6-
<artifactId>swordForOffer</artifactId>
7-
<version>0.0.1-SNAPSHOT</version>
8-
<packaging>jar</packaging>
5+
<groupId>com.netease.yangrubing</groupId>
6+
<artifactId>swordForOffer</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
99

10-
<name>swordForOffer</name>
11-
<url>http://maven.apache.org</url>
10+
<name>swordForOffer</name>
11+
<url>http://maven.apache.org</url>
1212

13-
<properties>
14-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
</properties>
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
1616

17-
<dependencies>
18-
<dependency>
19-
<groupId>junit</groupId>
20-
<artifactId>junit</artifactId>
21-
<version>4.7</version>
22-
<scope>test</scope>
23-
</dependency>
24-
</dependencies>
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>4.7</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
2525
</project>

swordForOffer/src/main/java/problem18/SubstructureInTree.java

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import utils.BinaryTreeNode;
44

55
/**
6-
* 树的子结构,判断
6+
* 树的子结构
77
* Created by bjyangrubing on 2016/8/17.
88
*/
99
public class SubstructureInTree
1010
{
11-
11+
/**输入两个树A, B,判断A是否包含B*/
1212
public boolean hasSubTree(BinaryTreeNode treeA, BinaryTreeNode treeB)
1313
{
14-
if (treeB == null)//首先判断如果,如果B为空树,返回true
14+
if (treeB == null)//首先判断如果B为空树,返回true
1515
return true;
1616
if (treeA == null)//如果B不为null,A为null返回false
1717
return false;
@@ -20,8 +20,9 @@ public boolean hasSubTree(BinaryTreeNode treeA, BinaryTreeNode treeB)
2020

2121
if (treeA != null && treeB != null)
2222
{
23+
//首先从根节点进行判断,如果值相同,则判断以当前节点为根节点的树A是否包含树B
2324
if (treeA.value == treeB.value)
24-
result = doesTreeAHasTreeB(treeA, treeB);//判断以当前节点为根节点的数是不是和treeB相同
25+
result = doesTreeAHasTreeB(treeA, treeB);//判断以当前节点为根节点的树是不是和treeB相同
2526
if (!result)
2627
result = hasSubTree(treeA.leftNode, treeB) || hasSubTree(treeA.rightNode, treeB);
2728
}
@@ -39,34 +40,4 @@ private boolean doesTreeAHasTreeB(BinaryTreeNode treeA, BinaryTreeNode treeB)
3940
//递归判断treeA和treeB的节点是否相同
4041
return doesTreeAHasTreeB(treeA.leftNode, treeB.leftNode) && doesTreeAHasTreeB(treeA.rightNode, treeB.rightNode);
4142
}
42-
43-
public static void main(String[] args)
44-
{
45-
BinaryTreeNode rootA = new BinaryTreeNode(8);
46-
BinaryTreeNode a_node1 = new BinaryTreeNode(8);
47-
BinaryTreeNode a_node2 = new BinaryTreeNode(7);
48-
BinaryTreeNode a_node3 = new BinaryTreeNode(9);
49-
BinaryTreeNode a_node4 = new BinaryTreeNode(2);
50-
BinaryTreeNode a_node5 = new BinaryTreeNode(4);
51-
BinaryTreeNode a_node6 = new BinaryTreeNode(7);
52-
rootA.leftNode = a_node1;
53-
rootA.rightNode = a_node2;
54-
55-
a_node1.leftNode = a_node3;
56-
a_node1.rightNode = a_node4;
57-
58-
a_node4.leftNode = a_node5;
59-
a_node4.rightNode = a_node6;
60-
61-
BinaryTreeNode rootB = new BinaryTreeNode(8);
62-
BinaryTreeNode b_node1 = new BinaryTreeNode(9);
63-
BinaryTreeNode b_node2 = new BinaryTreeNode(2);
64-
65-
rootB.leftNode = b_node1;
66-
rootB.rightNode = b_node2;
67-
68-
SubstructureInTree substructureInTree = new SubstructureInTree();
69-
boolean result = substructureInTree.hasSubTree(rootA, rootB);
70-
System.out.println(result);
71-
}
7243
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package problem19;
2+
3+
import utils.BinaryTreeNode;
4+
5+
/**二叉树的镜像
6+
* 输入一个二叉树,该函数输出他的镜像.
7+
* 二叉树的镜像定义:
8+
* 源二叉树
9+
8
10+
/ \
11+
6 10
12+
/ \ / \
13+
5 7 9 11
14+
镜像二叉树
15+
8
16+
/ \
17+
10 6
18+
/ \ / \
19+
11 9 7 5
20+
* Created by bjyangrubing on 2016/8/17.
21+
*/
22+
public class MirrorOfBinaryTree
23+
{
24+
25+
public void mirrorRecursively(BinaryTreeNode root)
26+
{
27+
if (root == null)
28+
return;
29+
//如果为叶子节点,则return
30+
if (root.leftNode == null && root.rightNode == null)
31+
return;
32+
33+
//交换左右节点
34+
BinaryTreeNode temp = root.leftNode;
35+
root.leftNode = root.rightNode;
36+
root.rightNode = temp;
37+
38+
if (root.leftNode != null)
39+
{
40+
//递归交换左子树
41+
mirrorRecursively(root.leftNode);
42+
}
43+
44+
if (root.rightNode != null)
45+
{
46+
//递归交换右子树
47+
mirrorRecursively(root.rightNode);
48+
}
49+
}
50+
51+
public static void main(String[] args)
52+
{
53+
54+
}
55+
56+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package problem20;
2+
3+
/**
4+
* 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字
5+
* 1 2 3 4
6+
* 5 6 7 8
7+
* 9 10 11 12
8+
* 13 14 15 16
9+
* 则打印1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
10+
* Created by bjyangrubing on 2016/8/17.
11+
*/
12+
public class PrintMatrix
13+
{
14+
15+
public void printMatrix(int[][] numbers, int rows, int columns)
16+
{
17+
if (numbers == null || rows <= 0 || columns <= 0)
18+
return;
19+
int start = 0;//打印的圈数
20+
//循环终止条件为columns > start*2 && rows>start*2
21+
while (columns > start * 2 && rows > start * 2)
22+
{
23+
printMatrixCycle(numbers, rows, columns, start);
24+
++start;
25+
}
26+
27+
}
28+
29+
private void printMatrixCycle(int[][] numbers, int rows, int columns, int start)
30+
{
31+
int endY = columns - 1 - start;
32+
int endX = rows - 1 - start;
33+
34+
//从左到右打印一行
35+
for (int i = start; i <= endY; i++)
36+
{
37+
int number = numbers[start][i];
38+
System.out.print(number + " ");
39+
}
40+
41+
//从上到下打印一行
42+
if (start < endX)
43+
{
44+
for (int i = start + 1; i <= endX; i++)
45+
{
46+
int number = numbers[i][endY];
47+
System.out.print(number + " ");
48+
}
49+
}
50+
51+
//从右到左打印一行
52+
if (start < endX && start < endY)
53+
{
54+
for (int i = endY - 1; i >= start; --i)
55+
{
56+
int number = numbers[endX][i];
57+
System.out.print(number + " ");
58+
}
59+
}
60+
61+
//从下到上打印一列
62+
if (start < endY && start < endX - 1)
63+
{
64+
for (int i = endX - 1; i >= start + 1; --i)
65+
{
66+
int number = numbers[i][start];
67+
System.out.print(number + " ");
68+
}
69+
}
70+
}
71+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package problem21;
2+
3+
import java.util.Stack;
4+
5+
/**定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
6+
* Created by bjyangrubing on 2016/8/17.
7+
*/
8+
public class MinInStack
9+
{
10+
private Stack<Integer> dataStack = new Stack<Integer>();
11+
/**辅助栈, 保存的是每次入栈时的daatStack中的最小值*/
12+
private Stack<Integer> minStack = new Stack<Integer>();
13+
14+
public void push(int node)
15+
{
16+
//将node压栈
17+
dataStack.push(node);
18+
//如果辅助栈为空,则直接压入节点
19+
if (minStack.isEmpty())
20+
{
21+
minStack.push(node);
22+
}
23+
else
24+
{
25+
//否则,辅助栈压入node和minStack.peek()中的最小值
26+
minStack.push(Math.min(minStack.peek(), node));
27+
}
28+
29+
}
30+
31+
public void pop()
32+
{
33+
dataStack.pop();
34+
minStack.pop();
35+
}
36+
37+
public int top()
38+
{
39+
return dataStack.peek();
40+
}
41+
42+
public int min()
43+
{
44+
return minStack.peek();
45+
}
46+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package problem22;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。
7+
* 例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。
8+
* (注意:这两个序列的长度是相等的)
9+
* Created by bjyangrubing on 2016/8/17.
10+
*/
11+
public class StackPushPopOrder {
12+
public boolean IsPopOrder(int[] pushA, int[] popA) {
13+
14+
if (pushA == null || popA == null)
15+
return false;
16+
//辅助栈
17+
Stack<Integer> stack = new Stack<Integer>();
18+
int point = 0;
19+
for (int i = 0; i < popA.length; i++) {
20+
//
21+
if (!stack.isEmpty() && popA[i] == stack.peek()) {
22+
stack.pop();
23+
} else {
24+
25+
if (point == pushA.length)
26+
return false;
27+
else {
28+
do {
29+
stack.push(pushA[point++]);
30+
} while (point != pushA.length && stack.peek() != popA[i]);
31+
32+
if (stack.peek() == popA[i]) {
33+
stack.pop();
34+
} else {
35+
return false;
36+
}
37+
}
38+
}
39+
}
40+
return true;
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package problem23;
2+
3+
import utils.BinaryTreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.LinkedList;
7+
import java.util.Queue;
8+
9+
/**从上往下打印出二叉树的每个节点,同层节点从左至右打印。二叉树的层次遍历
10+
* Created by bjyangrubing on 2016/8/17.
11+
*/
12+
public class PrintFromTopToBottom
13+
{
14+
15+
/*层次遍历二叉树
16+
* 借助辅助的队列,把左子树右子树分别加到FIFO的队列中,然后输出就行了
17+
* **/
18+
public ArrayList<Integer> printFromTopToBottom(BinaryTreeNode root)
19+
{
20+
if (root == null)
21+
return null;
22+
//LinkedList实现了Queue接口
23+
Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();
24+
queue.add(root);
25+
ArrayList<Integer> rtList = new ArrayList<Integer>();
26+
while (!queue.isEmpty())
27+
{
28+
BinaryTreeNode node = queue.poll();
29+
rtList.add(node.value);
30+
31+
if (node.leftNode != null)
32+
{
33+
queue.add(node.leftNode);
34+
}
35+
if (node.rightNode != null)
36+
{
37+
queue.add(node.rightNode);
38+
}
39+
}
40+
return rtList;
41+
}
42+
}

0 commit comments

Comments
 (0)