您的位置:首页 > 职场人生

经典面试题——二叉树

2016-05-18 21:35 423 查看
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

struct Node {
Node *l, *r;
int x;
Node(int x=0, Node *l=NULL, Node *r=NULL):x(x),l(l),r(r) {}
};

class Tree {
public:
Tree(Node* root) {
this->root = root;
}

Node* getRoot() {
return root;
}

Node* buildTree() {
int x;
cin >> x;
if(x != -1) {
Node* curRoot = new Node(x);
curRoot->l = buildTree();
curRoot->r = buildTree();
return curRoot;
} else {
return NULL;
}
}

void print_pre_order(Node* cur) {
if(cur == NULL)    return;
printf("%d ", cur->x);
print_pre_order(cur->l);
print_pre_order(cur->r);
}

void print_in_order(Node* cur) {
if(cur == NULL)    return;
print_in_order(cur->l);
printf("%d ", cur->x);
print_in_order(cur->r);
}

void print_post_order(Node* cur) {
if(cur == NULL)    return;
print_post_order(cur->l);
print_post_order(cur->r);
printf("%d ", cur->x);
}

void print_level_order(Node* cur) {
if(cur==NULL)    return;
queue<Node*> que;
que.push(cur);
while(!que.empty()) {
Node* c = que.front();
que.pop();
printf("%d ", c->x);
if(c->l)    que.push(c->l);
if(c->r)    que.push(c->r);
}
}

int getNodeNum(Node* root) {
if(root==NULL)    return 0;
return getNodeNum(root->l) + getNodeNum(root->r) + 1;
}

int getLeafNum(Node* root) {
if(root==NULL)    return 0;
if(root->l==NULL && root->r==NULL)    return 1; // 是叶子节点
return getLeafNum(root->l) + getLeafNum(root->r); // 非叶子节点
}

int getHeight(Node* root) {
if(root==NULL)    return 0;
return max(getHeight(root->l), getHeight(root->r))+1; // 每个节点都加1,所以统计了所有节点数
}

void turn_all_node(Node *root) {
if(root==NULL)    return;
if(root->l==NULL && root->r==NULL)    return; // 两个分支均为空,交换无意义
// 左右分支交换
Node* temp = root->l;
root->l = root->r;
root->r = temp;
if(root->l)    turn_all_node(root->l);
if(root->r)    turn_all_node(root->r);
}

bool isExist(Node *root, int x) {
if(root == NULL)    return 0;
if(root->x == x)    return 1;
return isExist(root->l, x) || isExist(root->r, x);
}
// 求二叉树第K层的节点个数
int getKnum(Node *root, int k) {
if(root==NULL || k < 1)    return 0;
if(k==1)    return 1;
return getKnum(root->l, k-1)+getKnum(root->r, k-1);
}
// 判断两棵二叉树是否结构相同
bool cmp(Node *a, Node *b) {
if(a == NULL && b == NULL) return 1;
if(a==NULL || b==NULL)    return 0;
return cmp(a->l, b->l) && cmp(a->r, b->r);
}
private:
Node *root;
};

int main ()
{
// 1 2 4 -1 -1 5 -1 6 -1 -1 3 7 -1 8 -1 -1 -1
// 6 4 2 3 -1 -1 -1 -1 5 1 -1 -1 7 -1 -1
Node* root;
Tree tree(root);
root = tree.buildTree();

// 前、中、后序遍历
tree.print_pre_order(root);
printf("\n");

tree.print_in_order(root);
printf("\n");

tree.print_post_order(root);
printf("\n");

tree.print_level_order(root);
printf("\n");

//节点个数
cout << "Node num: " << tree.getNodeNum(root) << endl;

// 叶子个数
cout << "Leaf num: " << tree.getLeafNum(root) << endl;

// 树高
cout << "Height: " << tree.getHeight(root) << endl;

// 二叉树镜像
tree.turn_all_node(root);
tree.print_level_order(root);
printf("\n");

//子树的节点查找
//    int x;
//    while(cin >> x) {
//        printf("%s\n", tree.isExist(root, x) ? "Yes!":"No.");
//    }

//求二叉树第K层的节点个数
//    int k;
//    while(cin >> k) {
//        printf("The %dth floor nodes num: %d\n", k, tree.getKnum(root, k));
//    }

return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: