2019年4月13日17:20:51 今天上午华为面试的手撕代码题目!感触良多!
// 前序遍历 ,递归写法
public void frontTraversal(treeNode node) {
if (node != null) {
System.out.print(node.data+" ");
frontTraversal(node.left);
frontTraversal(node.right);
}
}
//中序遍历 midTraversal,递归写法
public void midTraversal(treeNode node) {
if (node != null) {
midTraversal(node.left);
System.out.print(node.data+" ");
midTraversal(node.right);
}
}
//后序遍历,递归写法
public void afterTraversal(treeNode node) {
if (node != null) {
afterTraversal(node.left);
afterTraversal(node.right);
System.out.print(node.data+" ");
}
}
// 前序遍历,非递归写法,利用stack
public void frontTraversalNew(treeNode node) {
Stack<treeNode> stack =new Stack<>();
while(node != null || !stack.isEmpty()) {
while(node != null) {
System.out.print(node.data+" ");
stack.push(node);
node = node.left;
}
if (! stack.isEmpty()) {
node = stack.pop();
node = node.right;
}
}
}
//中序遍历,非递归写法,利用stack
public void midTraversalNew(treeNode node) {
Stack<treeNode> stack = new Stack<>();
while(node != null || !stack.isEmpty()) {
while(node != null) {
stack.push(node);
node = node.left;
}
if (!stack.isEmpty()) {
node = stack.pop();
System.out.print(node.data+" ");
node = node.right;
}
}
}
//后序遍历,非递归写法,利用stack两个
public void afterTraversalNew(treeNode node) {
Stack<treeNode> stackForNode = new Stack<>();
Stack< Integer> stackForState =new Stack<>();
int state =1;
while(node != null || !stackForNode.isEmpty()) {
while(node != null) {
stackForNode.push(node);
stackForState.push(0);
node = node.left;
}
while(!stackForNode.isEmpty() && stackForState.peek()==state) {
stackForState.pop();
//node = stackForNode.pop();
System.out.print(stackForNode.pop().data+" ");
}
if( !stackForNode.isEmpty()) {
stackForState.pop();
stackForState.push(1);
node = stackForNode.peek();
node = node.right;
}
}
}
补充一个小代码:图的深度优先遍历(DFS)
package dataStructure;
import java.util.Arrays;
import java.util.Stack;
public class GraphTest {
public static void main(String[] args) {
Vertex v1 =new Vertex('A');
Vertex v2 =new Vertex('B');
Vertex v3 =new Vertex('C');
Vertex v4 =new Vertex('D');
Vertex v5 =new Vertex('E');
Graph graph =new Graph(5);
graph.addVertex(v1);
graph.addVertex(v2);
graph.addVertex(v3);
graph.addVertex(v4);
graph.addVertex(v5);
graph.addEdge('A', 'B');
graph.addEdge('A', 'C');
graph.addEdge('A', 'D');
graph.addEdge('B', 'E');
graph.addEdge('C', 'E');
for(int [] a: graph.adjMat) {
System.out.println(Arrays.toString(a));
}
System.out.println("深度优先搜索的结果:");
graph.DFS();
}
}
class Vertex{
public char Value;
public boolean wasVisited = false;
public Vertex(char value) {
// TODO Auto-generated constructor stub
this.Value = value;
}
public char getValue() {
return Value;
}
}
class Graph{
private int GraphSize;
public Stack<Integer> stack = new Stack<>();
public int getGraphSize() {
return GraphSize;
}
public int currentSize;
public Vertex [] vertexs ;
public int[][] adjMat;
public Graph(int size) {
// TODO Auto-generated constructor stub
this.GraphSize = size;
vertexs = new Vertex[size];
adjMat = new int[size][size];
}
//向当前图中添加节点
public void addVertex(Vertex v) {
adjMat[currentSize][currentSize] = 1; //每次加入一个节点就将对角线 置为1
vertexs[currentSize++] = v;
}
public void addEdge(char a,char b) {
int index1 = 0;
for (int i=0;i < vertexs.length; i++) {
if (vertexs[i].Value == a) {
index1 = i;
break;
}
}
int index2 = 0;
for (int i=0;i < vertexs.length; i++) {
if (vertexs[i].Value == b) {
index2 = i;
break;
}
}
adjMat[index1][index2] = 1;
adjMat[index2][index1] = 1;
}
public void DFS() {
int maxDeep=1;
vertexs[0].wasVisited = true;
stack.push(0);
System.out.println(vertexs[0].Value);
while(! stack.isEmpty()) {
for(int i= stack.peek()+1;i<vertexs.length;i++) {
if (adjMat[stack.peek()][i] == 1 && vertexs[i].wasVisited == false) {
stack.push(i);
maxDeep=Math.max(maxDeep, stack.size());
vertexs[i].wasVisited = true;
System.out.println(vertexs[i].Value);
}
}
stack.pop();
}
System.out.println(maxDeep);
}
public int getCurrentSize() {
return currentSize;
}
}
单链表的逆序输出 + 单链表的逆向(都使用递归来做)
//使用递归,倒序输出链表
public void reversePrint(Node node) {
if (node.next == null ) {
System.out.print(node.getValue()+" ");
return;
}
reversePrint(node.next);
System.out.print(node.getValue()+" ");
}
//使用递归,实现链表的反转
/** https://blog.csdn.net/guyuealian/article/details/51119499
* @param head 当前链表的头结点
* @return
*/
public Node reverseLink(Node head) {
if (head == null || head.next == null) {
return head;
}
Node newHead = reverseLink(head.next);
head.next.next = head;
head.next=null;
return newHead;
}