#include <queue>
#include <list>
#include <iostream>
using namespace std;
typedef struct treenode {
char val;
struct treenode *left;
struct treenode *right;
treenode(char x) :
val(x), left(NULL), right(NULL) {
}
}treenode;
#include <queue>
#include <stack>
class Solution33{
public:
treenode *create(string first = "ABDCEF", string mid = "BDAEFC")
{
if (first == "\0"||mid=="\0")
{
return 0;
}
treenode *root = new treenode(first[0]);
int len = first.size();
string left_first, left_mid, right_first, right_mid;
int n = 0;
while (mid[n] != first[0])
{
++n;
}//2
left_first.assign(first, 1, n);
left_mid.assign(mid, 0, n);
right_first.assign(first, n+1, len - n);
right_mid.assign(mid, n + 1, len - n);
root->left = create(left_first, left_mid);
root->right = create(right_first, right_mid);
return root;
}
void last_print(treenode *root)
{
if (root != NULL)
{
last_print(root->left);
last_print(root->right);
cout << root->val;
}
}
void first_print(treenode *root)
{
if (root != NULL)
{
cout << root->val;
first_print(root->left);
first_print(root->right);
}
}
void mid_print(treenode *root)
{
if (root != NULL)
{
mid_print(root->left);
cout << root->val;
mid_print(root->right);
}
}
void ceng_print(treenode *root)
{
queue<treenode*> q;
q.push(root);
while (!q.empty())
{
treenode *temp = q.front();
q.pop();
cout << temp->val;
if (temp->left != NULL)
q.push(temp->left);
if (temp->right != NULL)
q.push(temp->right);
}
}
void k_ceng_print(treenode *root,int k)
{
if (root == NULL || k < 0)
return;
if (k == 1)//根从第0层算起
cout << root->val;
else
{
k_ceng_print(root->left, k - 1);
k_ceng_print(root->right, k - 1);
}
}
int node_size(treenode *root)
{
int num = 0;
queue<treenode*> q;
q.push(root);
++num;
while (!q.empty())
{
treenode *temp = q.front();
q.pop();
//cout << temp->val;
if (temp->left != NULL)
{
q.push(temp->left); num++;
}
if (temp->right != NULL)
{
q.push(temp->right); num++;
}
}
return num;
}
treenode * find_node(treenode *root, int value)
{
if (root == NULL)
return NULL;
if (root->val == value)
return root;
treenode * res = find_node(root->left, value);
if (res == NULL)
res = find_node(root->right, value);
return res;
}
treenode * find_parent_node(treenode *root,treenode *node)
{
if (root == node || root == NULL)
return NULL;
treenode *res = FUN_find_parent_node(root->left, node, root);
if (res == NULL)
res = FUN_find_parent_node(root->right, node, root);
return res;
}
treenode * FUN_find_parent_node(treenode *root, treenode *node,treenode* parent)
{
if (root == NULL)
return NULL;
if (root == node)
return parent;
treenode *res = FUN_find_parent_node(root->left, node, root);
if (res == NULL)
res = FUN_find_parent_node(root->right, node, root);
return res;
}
int leaf_node_num(treenode*root)
{
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 1;
return leaf_node_num(root->left) + leaf_node_num(root->right);
}
int nice_leaf_num(treenode *root)
{
if (root == NULL)
return 0;
int num = 0;
queue<treenode *> q;
q.push(root);
while (!q.empty())
{
root = q.front();
q.pop();
if (root->left == NULL &&root->right == NULL)
{
num++;
continue;
}
if (root->left)
q.push(root->left);
if (root->right)
q.push(root->right);
}
return num;
}
int tree_depth(treenode*root)
{
int l_depth = 0,r_depth=0;
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 1;
l_depth += tree_depth(root->left);
r_depth += tree_depth(root->right);
return l_depth > r_depth ? l_depth + 1 : r_depth + 1;
}
int nice_tree_depth(treenode *root)
{
if (root == NULL)
return 0;
int depth = 0;
queue<treenode *> q;
treenode *last = root;
q.push(root);
while (!q.empty())
{
root = q.front();
q.pop();
if (root->left)
q.push(root->left);
if (root->right)
q.push(root->right);
if (root == last)
{
depth++;
if (!q.empty())
last = q.back();
}
}
return depth;
}
treenode* common_parent(treenode *root, treenode *node1, treenode *node2)
{
if (root == NULL||node1==NULL||node2==NULL)
return NULL;
list<treenode *>q1, q2;
treenode *temp;
do{
temp=find_parent_node(root, node1);
if (temp != NULL)
q1.push_back(temp);
else
q1.push_back(root);//已找到根结点
node1 = temp;//等于自己父节点,继续找父节点的父节点。
}
while (q1.back() != root);
do{
temp = find_parent_node(root, node2);
if (temp != NULL)
q2.push_back(temp);
else
q2.push_back(root);//已找到根结点
node2 = temp;//等于自己父节点,继续找父节点的父节点。
} while (q2.back() != root);
for (list<treenode *>::iterator i = q1.begin(); i != q1.end(); i++)
{
for (list<treenode *>::iterator j = q2.begin(); j != q2.end(); j++)
{
if (*i == *j)
return *i;
}
}
return NULL;
}
void nice_first_print(treenode *root)
{
if (root == NULL)
return;
stack<treenode *>s;
treenode * p = root;
while (!s.empty() || p)
{
while (p != NULL)
{
cout << p->val;
s.push(p);
p = p->left;
}
if (!s.empty()){
p = s.top();
s.pop();
p = p->right;
}
}
}
void nice_mid_print(treenode *root)
{
if (root == NULL)
return;
stack<treenode *>s;
treenode * p = root;
while (!s.empty() || p)
{
while (p != NULL)
{
s.push(p);
p = p->left;
}
if (!s.empty()){
p = s.top();
s.pop();
cout << p->val;
p = p->right;
}
}
}
void nice_last_print(treenode* root)
{
if (root == NULL)
return ;
stack<treenode *> s1;
stack<treenode *> s2;
treenode *p = root;
while (p || !s1.empty())
{
while (p!=NULL)
{
s1.push(p);
s2.push(p);
p = p->right;
}
if (!s1.empty())
{
p = s1.top();
s1.pop();
p = p->left;
}
}
while (!s2.empty())
{
p = s2.top();
cout << p->val;
s2.pop();
}
return;
}
bool is_full_tree(treenode *root)
{
if (root == NULL)
return true;
queue<treenode *> q;
int num_node = 0;
int depth = 0;
treenode *p = root;
treenode *last = root;
q.push(p);
while (!q.empty())
{
p = q.front();
num_node++;
q.pop();
if (p->left != NULL)
{
q.push(p->left);
}
if (p->right != NULL)
{
q.push(p->right);
}
if (last == p)
{
depth++;
if (!q.empty() )
last = q.back();
}
}
if (num_node == pow(2, depth) - 1)
return true;
return false;
}
bool is_total_tree(treenode *root)
{
if (root == NULL)
return true;
queue<treenode *> q;
treenode *p = root;
treenode *last = root;
q.push(p);
while (!q.empty())
{
p = q.front();
q.pop();
if (p == NULL && !q.empty())
return false;
if (p->left == NULL && p->right==NULL)
{
continue;
}
q.push(p->left);
q.push(p->right);
}
return true;
}
};
class Solution {//判断一棵树是不是平衡二叉树
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot==NULL)
return true;
flag=true;
tree_depth(pRoot);
return flag;
}
int tree_depth(TreeNode*root)
{
int l_depth = 0,r_depth=0;
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 1;
l_depth += tree_depth(root->left);
r_depth += tree_depth(root->right);
if(abs(l_depth-r_depth)>1)
flag= false;
return l_depth > r_depth ? l_depth + 1 : r_depth + 1;
}
bool flag;
};
int main(){
Solution33 s;
treenode *root = s.create();//使用前序中序 创建一颗二叉树
s.first_print(root);//递归版的前序遍历
cout << endl;
s.mid_print(root);//递归版的中序遍历
cout << endl;
s.last_print(root);//递归版的后序遍历
cout << endl;
s.ceng_print(root);//循环版的层次遍历 使用一个队列
cout << endl;
s.k_ceng_print(root,3);//打印第k层 递归版
cout << endl;
treenode *temp = s.find_node(root, 'E'); //按值查找结点
cout << temp->val << endl;
temp=s.find_parent_node(root, root->left->right);//查找结点的双亲结点
cout << temp->val << endl;
cout << s.node_size(root) << endl; //结点个数
cout << s.leaf_node_num(root) << endl;//叶子结点个数
cout << s.tree_depth(root) << endl;//树的深度
temp = s.common_parent(root, root->left->right, root->right->left->right);
cout << temp->val << endl;//查找两个结点的共同双亲
return 0;
}