1 #include <iostream>
2 #include <cmath>
3 #include <queue>
4 #include <stack>
5 using namespace std;
6
7 struct Treenode
8 {
9 int data;
10 Treenode *left, *right;
11 };
12 // 求树高
13 int height(Treenode* root)
14 {
15 if(!root)
16 return 0;
17 int leftHeight=height(root->left);
18 int rightHeight=height(root->right);
19
20 return (leftHeight>rightHeight)?leftHeight+1:rightHeight+1;
21 }
22
23 // 判断是否平衡二叉树
24 /*
25 平衡二叉树的子树也都是平衡的
26 */
27 bool IsBlance(Treenode* root)
28 {
29 if(!root)
30 return true;
31 int leftHeight=height(root->left);
32 int rightHeight=height(root->right);
33 int diff=leftHeight-rightHeight;
34 if(abs(diff)>1)
35 return false;
36 return IsBlance(root->left) && IsBlance(root->right);
37 }
38
39 // 判断是否完全二叉树
40 // 层次遍历,出现空节点后面应该都是NULL
41 bool IsComplate(Treenode* root)
42 {
43 if(!root)
44 return true;
45 queue<Treenode*>q;
46 bool flag=true; // 若出现NULL后继置为false
47 q.push(root);
48 while(!root)
49 {
50 Treenode* p=q.front();
51 q.pop();
52 if(p->left && flag)q.push(p->left);
53 else if(p->left) return false;
54 else flag=false;
55
56 if(p->right && flag)q.push(p->right);
57 else if(p->right) return false;
58 else flag=false;
59 }
60 return true;
61 }
62
63 // 判断是否BST二叉排序树
64 //中序遍历,是从小到大有序排列
65 bool IsSort(Treenode* root)
66 {
67 if(!root)
68 return true;
69 stack<Treenode*>s;
70 Treenode* p=root;
71 Treenode* pre=NULL; // 记录中序遍历的前一个节点
72 while(p || !s.empty())
73 {
74 while(p) // 走左子树
75 {
76 s.push(p);
77 p=p->left;
78 }
79 if(!s.empty())
80 {
81 p=s.top(); //弹出最左边的节点
82 s.pop();
83 if(pre && pre->left > p->left) return false;
84 pre=p;
85 p=p->left;
86 }
87 }
88 return true;
89 }
90
91 //树的遍历
92 //递归
93 void PreOrder(Treenode* root)
94 {
95 if(!root)
96 return;
97 cout<<root->data;
98 PreOrder(root->left);
99 PreOrder(root->right);
100 }
101
102 void InOrder(Treenode* root)
103 {
104 if(!root)
105 return;
106 InOrder(root->left);
107 cout<<root->data;
108 InOrder(root->right);
109 }
110
111 void PostOrder(Treenode* root)
112 {
113 if(!root)
114 return;
115 PostOrder(root->left);
116 PostOrder(root->right);
117 cout<<root->data;
118 }
119
120 // 非递归
121 void LevelOrder(Treenode* root)
122 {
123 if(!root)
124 return;
125 queue<Treenode*>q;
126 q.push(root);
127 while(!q.empty())
128 {
129 Treenode* p=q.front();q.pop();
130 cout<<p->data;
131 if(p->left)q.push(p->left);
132 if(p->right)q.push(p->right);
133 }
134 }
135
136 void PreOrder2(Treenode* root)
137 {
138 if(!root)
139 return;
140 stack<Treenode*>q;
141 Treenode* p=root;
142 while(p || !q.empty())
143 {
144 while(p) // 一直往左分支走
145 {
146 cout<<p->data;
147 q.push(p);
148 p=p->left;
149 }
150 if(!q.empty()) //回溯一步往右走
151 {
152 p=q.top();
153 q.pop();
154 p=p->right;
155 }
156 }
157 }
158
159 void InOrder2(Treenode* root)
160 {
161 if(!root)
162 return;
163 stack<Treenode*>q;
164 Treenode* p=root;
165 while(p || !q.empty())
166 {
167 while(p)
168 {
169 q.push(p);
170 p=p->left;
171 }
172 if(!q.empty()) // 退栈走右子树
173 {
174 p=q.top();
175 q.pop();
176 cout<<p->data;
177 p=p->right;
178 }
179 }
180 }
181
182 void PostOrder2(Treenode* root) //***想通
183 {
184 if(!root)
185 return;
186 stack<Treenode*>s;
187 Treenode* p=root;
188 Treenode* q=NULL;
189 while(p || !s.empty())
190 {
191 while(p)
192 {
193 s.push(p);
194 p=p->left;
195 }
196 if(!s.empty())
197 {
198 p=s.top();
199 if(p->right==NULL || p->right==q)// p是叶子节点或p是刚打印节点的中间节点
200 {
201 cout<<p->data;
202 q=p; // 记录返回时要用的中间节点
203 s.pop();
204 p=NULL;
205 }
206 else
207 p=p->right; // p不是叶子节点,不出栈,往右走
208 }
209 }
210 }