Skip to content

Commit 50d36d5

Browse files
author
chenweijie
committed
重建二叉树,两个栈模拟一个队列
1 parent 173b828 commit 50d36d5

File tree

6 files changed

+216
-14
lines changed

6 files changed

+216
-14
lines changed

src/main/java/com/chen/algorithm/exercise/BinaryTree.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.chen.algorithm.exercise.exercise4;
2+
3+
/**
4+
* 重建二叉树
5+
* https://www.nowcoder.com/profile/566744/codeBookDetail?submissionId=1516321
6+
*
7+
* @author : chen weijie
8+
* @Date: 2019-02-23 1:02 AM
9+
*/
10+
public class BinaryTree {
11+
12+
13+
public static class TreeNode {
14+
15+
int val;
16+
17+
TreeNode left;
18+
19+
TreeNode right;
20+
21+
TreeNode(int x) {
22+
val = x;
23+
}
24+
}
25+
26+
27+
public static TreeNode reConstructBTree(int[] pre, int[] in) {
28+
TreeNode root = reConstructBTree(pre, 0, pre.length - 1, in, 0, in.length - 1);
29+
return root;
30+
}
31+
32+
33+
private static TreeNode reConstructBTree(int[] pre, int startPre, int endPre, int[] in, int startIn, int endIn) {
34+
35+
if (startPre > endPre || startIn > endIn) {
36+
return null;
37+
}
38+
39+
TreeNode root = new TreeNode(pre[startPre]);
40+
for (int i = startIn; i <= endIn; i++) {
41+
if (in[i] == pre[startPre]) {
42+
root.left = reConstructBTree(pre, startPre + 1, startPre + i - startIn, in, startIn, i - 1);
43+
root.right = reConstructBTree(pre, i - startIn + startPre + 1, endPre, in, i + 1, endIn);
44+
}
45+
}
46+
47+
48+
return root;
49+
}
50+
51+
52+
/**
53+
* 前序遍历
54+
*
55+
* @param current
56+
*/
57+
public static void preOrder(TreeNode current) {
58+
if (current != null) {
59+
System.out.println(current.val + " ");
60+
preOrder(current.left);
61+
preOrder(current.right);
62+
}
63+
}
64+
65+
66+
public static void main(String[] args) {
67+
int[] pre = {1, 2, 4, 7, 3, 5, 6, 8};
68+
int[] in = {4, 7, 2, 1, 5, 3, 8, 6};
69+
TreeNode node = reConstructBTree(pre, in);
70+
preOrder(node);
71+
}
72+
73+
74+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.chen.algorithm.exercise.exercise5;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-04-16 00:36
6+
*/
7+
public class MyStack {
8+
9+
10+
int [] elementData;
11+
12+
int maxSize;
13+
14+
int top;
15+
16+
17+
public MyStack(int maxSize){
18+
elementData = new int[maxSize];
19+
this.maxSize =maxSize;
20+
top = -1;
21+
}
22+
23+
24+
public int pop(){
25+
return elementData[top--];
26+
}
27+
28+
29+
public void push(int data) {
30+
if (top < maxSize - 1) {
31+
elementData[++top] = data;
32+
}
33+
}
34+
35+
36+
public boolean isEmpty() {
37+
return top == -1;
38+
}
39+
40+
41+
42+
43+
44+
45+
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.chen.algorithm.exercise.exercise5;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-04-16 00:44
6+
*/
7+
public class SimilarQueen {
8+
9+
10+
MyStack stack1 = new MyStack(10);
11+
12+
MyStack stack2 = new MyStack(10);
13+
14+
15+
public void push(int data) {
16+
stack1.push(data);
17+
}
18+
19+
20+
public int pop() {
21+
22+
if (stack1.isEmpty() && stack2.isEmpty()) {
23+
throw new RuntimeException("element is empty!");
24+
}
25+
26+
//只要栈2不为空就pop。如果栈2位空且栈1不为空,则从栈1中取出放入栈2,然后从栈2中取出。
27+
if (stack2.isEmpty()) {
28+
while (!stack1.isEmpty()) {
29+
stack2.push(stack1.pop());
30+
}
31+
}
32+
return stack2.pop();
33+
}
34+
35+
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.chen.test;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-04-15 23:18
6+
*/
7+
public class ListNode {
8+
9+
10+
int val;
11+
12+
ListNode next = null;
13+
14+
public ListNode(int val) {
15+
this.val = val;
16+
}
17+
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.chen.test;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author : chen weijie
8+
* @Date: 2019-04-15 23:18
9+
*/
10+
public class Solution {
11+
12+
13+
public List<Integer> outList(ListNode node) {
14+
15+
if (node == null) {
16+
return new ArrayList<>();
17+
}
18+
19+
ListNode head = node;
20+
ListNode current = node.next;
21+
22+
while (current != null) {
23+
ListNode temp = current.next;
24+
current.next = head;
25+
head = current;
26+
current = temp;
27+
}
28+
29+
node.next = null;
30+
31+
List<Integer> res = new ArrayList<>();
32+
while (head != null) {
33+
res.add(head.val);
34+
head = head.next;
35+
}
36+
return res;
37+
}
38+
39+
40+
41+
42+
}

0 commit comments

Comments
 (0)