经典面试题——二叉树
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; }
相关文章推荐
- Java面试题及答案(一)
- 这年头,谁没被坑过(致敬给一线程序员)
- 绿盟面试
- 面试常用排序算法
- 程序员的批评与自我批评(转)
- 为什么我不想成为Web前端程序员
- 絮絮叨叨,码农中的唐僧
- 剑指Offer----面试题一:为类添加赋值运算符函数
- 阿里巴巴2016前端开发实习生面试一面面经(总结)
- 面试的几个小问题?
- 奋斗吧,程序员——第三十九章 人生不失意,焉能慕知己
- 嵌入式工程师开发工作主要做些什么,以后收入怎样
- 最近5年133个Java面试问题列表
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- 面试:数组:三角形
- 职场中如何处理与领导的关系
- 程序员平时该如何学习来提升自己的技术
- To Java程序员:切勿用普通for循环遍历LinkedList
- 面试题22
- 如何成为一名优秀的程序员