-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathTreePrint.java
144 lines (131 loc) · 2.92 KB
/
TreePrint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.algorithdemo.tree;
import java.util.Stack;
public class TreePrint {
/**
* 递归 前序遍历
* @param head
*/
public static void preOrderRecur(TreeNode head){
if(head==null){
return;
}
System.out.print(head.getValue()+" ");
preOrderRecur(head.getLeft());
preOrderRecur(head.getRight());
}
/**
* 递归中序遍历
* @param head
*/
public static void inOrderRecur(TreeNode head){
if(head==null){
return;
}
inOrderRecur(head.getLeft());
System.out.print(head.getValue()+" ");
inOrderRecur(head.getRight());
}
/***
* 递归 后序遍历
* @param head
*/
public static void posOrderRecur(TreeNode head){
if(head == null){
return;
}
posOrderRecur(head.getLeft());
posOrderRecur(head.getRight());
System.out.print(head.getValue() + " ");
}
/**
* 前序遍历 非递归
* @param head
*/
public static void preOrderUnRecur(TreeNode head){
System.out.println("pre-order");
if(head!=null){
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.add(head);
while(!stack.isEmpty()){
head = stack.pop();
System.out.print(head.getValue()+" ");
if(head.getRight() != null){
stack.push(head.getRight());
}
if(head.getLeft() != null){
stack.push(head.getLeft());
}
}
}
}
/**
* 中序遍历 非递归
* @param head
*/
public static void inOrderUnRecur(TreeNode head){
System.out.println("in-order :");
if(head != null){
Stack<TreeNode> stack = new Stack<TreeNode>();
while(!stack.isEmpty() || head!=null ){
if(head!=null){
stack.push(head);
head = head.getLeft();
}else{
head = stack.pop();
System.out.print(head.getValue()+" ");
head = head.getRight();
}
}
}
}
/**
* 后序遍历 非递归
* @param head
*/
public static void posOrderUnRecur(TreeNode head){
System.out.println("pos-order:");
if(head!=null){
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(head);
TreeNode c = null;
while(!stack.isEmpty()){
c= stack.peek();
if(c.getLeft()!=null && head!=c.getLeft() && head!=c.getRight()){
stack.push(c.getLeft());
}else if(c.getRight()!=null && head!=c.getRight()){
stack.push(c.getRight());
}else{
System.out.print(stack.pop().getValue()+" ");
head = c;
}
}
}
}
/**
* 二叉树的深度
* @param root
* @return
*/
public static int TreeDepth(TreeNode root) {
if(root==null){
return 0;
}else{
int left = 1;
int right = 1;
left+=TreeDepth(root.getLeft());
right+=TreeDepth(root.getRight());
return left>right?left:right;
}
}
public static void main(String[] args) {
TreeNode tree = new TreeNode(1);
TreeNode lt = new TreeNode(2);
TreeNode rt = new TreeNode(3);
tree.setLeft(lt);
tree.setRight(rt);
inOrderRecur(tree);
preOrderRecur(tree);
inOrderUnRecur(tree);
System.out.println(TreeDepth(tree));
}
}