1102. Invert a Binary Tree (25)
2015-09-25 07:53
239 查看
1102 . Invert a Binary Tree (25)
The following is from Max Howell @twitter:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
Now it’s your turn to prove that YOU CAN invert a binary tree!
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node from 0 to N-1, and gives the indices of the left and right children of the node. If the child does not exist, a “-” will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.
Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1
二叉树的相关操作,用到并查集,递归,队列
The following is from Max Howell @twitter:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
Now it’s your turn to prove that YOU CAN invert a binary tree!
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node from 0 to N-1, and gives the indices of the left and right children of the node. If the child does not exist, a “-” will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.
Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1
二叉树的相关操作,用到并查集,递归,队列
#include <cstdio> #include <cstdlib> #include <iostream> #include <vector> #include <queue> using namespace std; #define N 20 int n ; struct data { int left ; int right ; }; data dt ; int fa ; int find(int x) { if(x == fa[x]) return x; return fa[x] = find(fa[x]) ; } void merg(int x , int y) { fa[find(y)] = find(x); } typedef struct node{ int data; struct node* left; struct node* right ; node(int _data = - 1) { data = _data ; } }Bnode ; Bnode* createTree(Bnode* root , int num) { root = new node(num); if(dt[num].left == -1) root->left = NULL ; else root->left = createTree( root->left , dt[num].left); if(dt[num].right == -1) root->right = NULL ; else root->right = createTree( root->right , dt[num].right); return root ; } void levelorder(Bnode* root) { queue<Bnode*> que; que.push(root) ; bool flag = false; while( !que.empty()) { Bnode* bt = que.front(); que.pop(); if(flag == false) { printf("%d",bt->data); flag = true; }else{ printf(" %d",bt->data); } if(bt->right != NULL) { que.push(bt->right); } if(bt->left != NULL) { que.push(bt->left); } } printf("\n"); } Bnode* reverse(Bnode* root) { if(root == NULL) return NULL; reverse(root->left); reverse(root->right); Bnode* tmp = root->left ; root->left = root->right; root->right = tmp ; return root ; } vector<int> in; void inorder(Bnode* root2) { if(root2 != NULL) { inorder(root2->left); in.push_back(root2->data); inorder(root2->right); } } int main() { //freopen("in.txt","r",stdin); scanf("%d\n",&n); int i ; char a , b ; for(i = 0 ; i< n ;i++) { fa[i] = i ; } for(i = 0 ; i < n ;i++) { scanf("%c %c\n",&a,&b); if(a == '-') { dt[i].left = -1 ; }else{ dt[i].left = a - '0' ; if(find(i) != find(dt[i].left)) merg(i,dt[i].left); } if(b == '-') { dt[i].right = -1 ; }else{ dt[i].right = b - '0' ; if(find(i) != find(dt[i].right)) merg(i,dt[i].right); } } int rootNum = find(0) ; Bnode* root = NULL; root = createTree( root , rootNum); levelorder(root); Bnode* root2 = reverse(root); in.clear(); inorder(root2); printf("%d",in[0]); for(i = 1;i<n;i++) { printf(" %d",in[i]); } printf("\n"); return 0 ; }
相关文章推荐
- 黑马程序员--Java基础学习笔记【构造器、静态】
- 缩略图
- 面试题2 编程实现单链表删除节点
- 黑马程序员--Java基础学习笔记【继承、多态】
- “花儿日记”项目总结
- “花儿日记”项目总结
- Linux内核学习之路:认识内核版本
- 黑马程序员--Java基础学习笔记【抽象类、接口】
- 【IOS 开发学习总结-OC-18】★★objective-c面向对象之——OC的包装类与对象处理
- 斐波那契数列
- 数据结构与算法分析:哈希表
- TouchAndGuest触摸事件和手势
- Learning Bluemix 有新 Logo 了
- c++课堂
- 谷歌公布放弃 RC4 和 SSLv3 的详细计划
- C++ 多态(一)
- 世界各国时区表 -- 与北京时差
- javaWebSerivice学习篇3-WSDL文档分析
- WPF如何实现一款类似360安全卫士界面的程序?(共享源码!)
- 页头--page-header