Skip to content

Commit 770f1ed

Browse files
committed
problem 18
1 parent fd3f41e commit 770f1ed

File tree

11 files changed

+306
-95
lines changed

11 files changed

+306
-95
lines changed

swordForOffer/src/main/java/problem06/ContructBinaryTree.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package problem06;
22

3+
import utils.BinaryTreeNode;
4+
35
import java.security.InvalidParameterException;
46
import java.util.Arrays;
57

6-
import utils.BinaryTreeNode;
7-
88
/**
99
* 输入前序和中序遍历结果,重建二叉树
1010
* @author bjyangrubing
@@ -44,8 +44,7 @@ public static void preOrder(BinaryTreeNode root)
4444
{
4545
if (root != null)
4646
{
47-
if (root.value != null)
48-
System.out.println(root.value);
47+
System.out.println(root.value);
4948
preOrder(root.leftNode);
5049
preOrder(root.rightNode);
5150
}
@@ -61,8 +60,7 @@ public static void inOrder(BinaryTreeNode root)
6160
if (root != null)
6261
{
6362
inOrder(root.leftNode);
64-
if (root.value != null)
65-
System.out.println(root.value);
63+
System.out.println(root.value);
6664
inOrder(root.rightNode);
6765
}
6866

swordForOffer/src/main/java/problem15/FindKthToTail.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,37 @@
1010
* 3. 如果输入的k大于链表的长度,则返回null
1111
* Created by yrb on 2016/8/16.
1212
*/
13-
public class FindKthToTail {
13+
public class FindKthToTail
14+
{
1415

15-
public ListNode findKthToTail(ListNode head, int k) {
16-
if (k <= 0 || head == null) {
17-
return null;
18-
}
16+
public ListNode findKthToTail(ListNode head, int k)
17+
{
18+
if (k <= 0 || head == null)
19+
{
20+
return null;
21+
}
1922

20-
//设置两个指针,一个快,一个慢
21-
ListNode aHead = head;
22-
ListNode behind = null;
23-
//快的先走k-1步
24-
for (int i = 0; i < k - 1; i++) {
25-
if (aHead.next != null)
26-
aHead = aHead.next;
27-
else
28-
return null;//如果k大于链表的长度,则直接返回null
29-
}
30-
//这时慢的指向第一个节点
31-
behind = head;
32-
//快的和慢的一起走,当快的走到末尾的时候,慢的指向的就是倒数第K个
33-
while (aHead.next != null) {
23+
//设置两个指针,一个快,一个慢
24+
ListNode aHead = head;
25+
ListNode behind = null;
26+
//快的先走k-1步
27+
for (int i = 0; i < k - 1; i++)
28+
{
29+
if (aHead.next != null)
30+
aHead = aHead.next;
31+
else
32+
return null;//如果k大于链表的长度,则直接返回null
33+
}
34+
//这时慢的指向第一个节点
35+
behind = head;
36+
//快的和慢的一起走,当快的走到末尾的时候,慢的指向的就是倒数第K个
37+
while (aHead.next != null)
38+
{
3439

35-
aHead = aHead.next;
36-
behind = behind.next;
37-
}
38-
return behind;
39-
}
40+
aHead = aHead.next;
41+
behind = behind.next;
42+
}
43+
return behind;
44+
}
4045

4146
}

swordForOffer/src/main/java/problem16/ReverseList.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@
66
* 反转链表
77
* Created by yrb on 2016/8/16.
88
*/
9-
public class ReverseList {
9+
public class ReverseList
10+
{
1011

11-
public ListNode reverseList(ListNode pHead) {
12-
ListNode pReverseHead = null;//反转后的头
13-
ListNode pNode = pHead;//当前节点
14-
ListNode pPrev = null;//当前节点的前一个节点
12+
public ListNode reverseList(ListNode pHead)
13+
{
14+
ListNode pReverseHead = null;//反转后的头
15+
ListNode pNode = pHead;//当前节点
16+
ListNode pPrev = null;//当前节点的前一个节点
1517

16-
while (pNode != null) {
17-
ListNode pNext = pNode.next;
18-
if (pNext == null)
19-
pReverseHead = pNode;
20-
pNode.next = pPrev;
21-
pPrev = pNode;
22-
pNode = pNext;
23-
}
24-
return pReverseHead;
25-
}
18+
while (pNode != null)
19+
{
20+
ListNode pNext = pNode.next;
21+
if (pNext == null)
22+
pReverseHead = pNode;
23+
pNode.next = pPrev;
24+
pPrev = pNode;
25+
pNode = pNext;
26+
}
27+
return pReverseHead;
28+
}
2629

2730
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package problem17;
2+
3+
import utils.ListNode;
4+
5+
/**合并两个排序的列表
6+
* 合并两个链表并使新链表中的节点仍然是递增排序的
7+
* Created by bjyangrubing on 2016/8/17.
8+
*/
9+
public class MergeSortedLists
10+
{
11+
12+
public ListNode merge(ListNode head1, ListNode head2)
13+
{
14+
15+
//递归的基准情况
16+
if (head1 == null)
17+
return head2;
18+
else if (head2 == null)
19+
return head1;
20+
21+
ListNode mHead = null;
22+
//如果head1的值<head2的值,则将合并后的头设置为head1
23+
if (head1.value < head2.value)
24+
{
25+
mHead = head1;
26+
mHead.next = merge(head1.next, head2);//递归合并剩余链表
27+
}
28+
else
29+
{
30+
mHead = head2;
31+
mHead.next = merge(head1, head2.next);
32+
}
33+
return mHead;
34+
}
35+
36+
}
632 KB
Loading
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package problem18;
2+
3+
import utils.BinaryTreeNode;
4+
5+
/**
6+
* 树的子结构,判断
7+
* Created by bjyangrubing on 2016/8/17.
8+
*/
9+
public class SubstructureInTree
10+
{
11+
12+
public boolean hasSubTree(BinaryTreeNode treeA, BinaryTreeNode treeB)
13+
{
14+
if (treeB == null)//首先判断如果,如果B为空树,返回true
15+
return true;
16+
if (treeA == null)//如果B不为null,A为null返回false
17+
return false;
18+
19+
boolean result = false;
20+
21+
if (treeA != null && treeB != null)
22+
{
23+
if (treeA.value == treeB.value)
24+
result = doesTreeAHasTreeB(treeA, treeB);//判断以当前节点为根节点的数是不是和treeB相同
25+
if (!result)
26+
result = hasSubTree(treeA.leftNode, treeB) || hasSubTree(treeA.rightNode, treeB);
27+
}
28+
return result;
29+
}
30+
31+
private boolean doesTreeAHasTreeB(BinaryTreeNode treeA, BinaryTreeNode treeB)
32+
{
33+
if (treeB == null)//首先判断如果,如果B为空树,返回true
34+
return true;
35+
if (treeA == null)//如果B不为null,A为null返回false
36+
return false;
37+
if (treeA.value != treeB.value)//如果两个节点的值不相同,则直接返回false
38+
return false;
39+
//递归判断treeA和treeB的节点是否相同
40+
return doesTreeAHasTreeB(treeA.leftNode, treeB.leftNode) && doesTreeAHasTreeB(treeA.rightNode, treeB.rightNode);
41+
}
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+
}
72+
}

swordForOffer/src/main/java/utils/BinaryTreeNode.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22

33
public class BinaryTreeNode
44
{
5-
public Integer value;
5+
public int value;
6+
7+
public BinaryTreeNode()
8+
{
9+
}
10+
11+
public BinaryTreeNode(int value)
12+
{
13+
this.value = value;
14+
}
615

716
public BinaryTreeNode leftNode;
817

swordForOffer/src/test/java/problem15/FindKthToTailTest.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,34 @@
77
/**
88
* Created by yrb on 2016/8/16.
99
*/
10-
public class FindKthToTailTest {
11-
@Test
12-
public void test() {
13-
ListNode six = new ListNode();
14-
ListNode one = new ListNode();
15-
ListNode two = new ListNode();
16-
ListNode three = new ListNode();
17-
ListNode four = new ListNode();
18-
ListNode five = new ListNode();
19-
one.value = 1;
20-
two.value = 2;
21-
three.value = 3;
22-
four.value = 4;
23-
five.value = 5;
24-
six.value = 6;
25-
one.next = two;
26-
two.next = three;
27-
three.next = four;
28-
four.next = five;
29-
five.next = six;
30-
Assert.assertEquals(3, new FindKthToTail().findKthToTail(one, 4).value);
10+
public class FindKthToTailTest
11+
{
12+
@Test
13+
public void test()
14+
{
15+
ListNode six = new ListNode();
16+
ListNode one = new ListNode();
17+
ListNode two = new ListNode();
18+
ListNode three = new ListNode();
19+
ListNode four = new ListNode();
20+
ListNode five = new ListNode();
21+
one.value = 1;
22+
two.value = 2;
23+
three.value = 3;
24+
four.value = 4;
25+
five.value = 5;
26+
six.value = 6;
27+
one.next = two;
28+
two.next = three;
29+
three.next = four;
30+
four.next = five;
31+
five.next = six;
32+
Assert.assertEquals(3, new FindKthToTail().findKthToTail(one, 4).value);
3133

32-
Assert.assertEquals(null, new FindKthToTail().findKthToTail(one, 0));
34+
Assert.assertEquals(null, new FindKthToTail().findKthToTail(one, 0));
3335

34-
Assert.assertEquals(null, new FindKthToTail().findKthToTail(one, 8));
36+
Assert.assertEquals(null, new FindKthToTail().findKthToTail(one, 8));
3537

36-
Assert.assertEquals(null, new FindKthToTail().findKthToTail(null, 1));
37-
}
38+
Assert.assertEquals(null, new FindKthToTail().findKthToTail(null, 1));
39+
}
3840
}

swordForOffer/src/test/java/problem16/ReverseListTest.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,33 @@
77
/**
88
* Created by yrb on 2016/8/16.
99
*/
10-
public class ReverseListTest {
11-
@Test
12-
public void test() {
13-
ListNode six = new ListNode();
14-
ListNode one = new ListNode();
15-
ListNode two = new ListNode();
16-
ListNode three = new ListNode();
17-
ListNode four = new ListNode();
18-
ListNode five = new ListNode();
19-
one.value = 1;
20-
two.value = 2;
21-
three.value = 3;
22-
four.value = 4;
23-
five.value = 5;
24-
six.value = 6;
25-
one.next = two;
26-
two.next = three;
27-
three.next = four;
28-
four.next = five;
29-
five.next = six;
30-
new ReverseList().reverseList(one);
10+
public class ReverseListTest
11+
{
12+
@Test
13+
public void test()
14+
{
15+
ListNode six = new ListNode();
16+
ListNode one = new ListNode();
17+
ListNode two = new ListNode();
18+
ListNode three = new ListNode();
19+
ListNode four = new ListNode();
20+
ListNode five = new ListNode();
21+
one.value = 1;
22+
two.value = 2;
23+
three.value = 3;
24+
four.value = 4;
25+
five.value = 5;
26+
six.value = 6;
27+
one.next = two;
28+
two.next = three;
29+
three.next = four;
30+
four.next = five;
31+
five.next = six;
32+
new ReverseList().reverseList(one);
3133

32-
Assert.assertEquals(1, two.next.value);
34+
Assert.assertEquals(1, two.next.value);
3335

34-
Assert.assertEquals(5, six.next.value);
36+
Assert.assertEquals(5, six.next.value);
3537

36-
}
38+
}
3739
}

0 commit comments

Comments
 (0)