1102. Invert a Binary Tree (25)
来源:互联网 发布:阿里云怎么代理加盟 编辑:程序博客网 时间:2024/06/03 21:11
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 <bits/stdc++.h>using namespace std;const int maxn = 100;struct node { int data, lchild, rchild; node(): data(-1), lchild(-1), rchild(-1){}}nodes[maxn];bool isNode[maxn] = { true };int n;void init() { for (int i = 0; i < maxn; i++) { isNode[i] = true; }}void LevelOrderTraversal(int root) { queue<node> q; q.push(nodes[root]); int cnt = 0; while (!q.empty()) { node kfront = q.front(); cnt++; printf("%d%c", kfront.data, (cnt == n) ? '\n':' '); q.pop(); if (kfront.rchild != -1) { q.push(nodes[kfront.rchild]); } if (kfront.lchild != -1) { q.push(nodes[kfront.lchild]); } }}void InOrderTraversal(int root) { int cnt = 0; stack<node> s; node p = nodes[root]; node temp; while (p.data != -1 || !s.empty()) { if (p.data != -1) { temp = p; s.push(temp); if (p.rchild != -1) p = nodes[p.rchild]; else p.data = -1; } else { if (!s.empty()) { temp = s.top(); s.pop(); p = temp; cnt++; printf("%d%c", p.data, (cnt == n) ? '\n':' '); if (p.lchild != -1) p = nodes[p.lchild]; else p.data = -1; } } }}int main(){ init(); scanf("%d", &n); char left[3], right[3]; for (int i = 0; i < n; i++) { scanf("%s %s", left, right); if (left[0] != '-') { nodes[i].lchild = atoi(left); isNode[atoi(left)] = false; } if (right[0] != '-') { nodes[i].rchild = atoi(right); isNode[atoi(right)] = false; } } int root; for (int i = 0; i < n; i++) { if (isNode[i] == true) root = i; } for (int i = 0; i < n; i++) { nodes[i].data = i; } LevelOrderTraversal(root); InOrderTraversal(root); return 0;}
- 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- [PAT]1102. Invert a Binary Tree (25)
- 【PAT】1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- PAT 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- PAT 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- pat 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- PAT_A 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- 1102. Invert a Binary Tree (25)
- Rikka with Candies(HDU6085) (bitset加速计数)
- 从1到n整数中1出现的次数:O(logn)算法
- 1413: StarFarming [最短路]
- 安装CocoaPods
- 【翻译】What the f*ck JavaScript?(JavaScript你怎么这样啊???)
- 1102. Invert a Binary Tree (25)
- 文件锁
- 数据结构概述
- 火狐flash
- Java关键字final、static使用总结
- 判断链表是否有环,并返回环的起始节点
- 控制台调试android命令
- AtCoder Beginner Contest 070 Transit Tree Path(一道鸡贼的最短路径题)
- 接触qt来第一个坑:Could not create directory "E:\oysl\QT\Error in " Util.asciify("build-untitle