1110. Complete Binary Tree 解析
来源:互联网 发布:adobe破解软件使用 编辑:程序博客网 时间:2024/06/17 02:36
刚开始像之前那个题一样,看到只有20个结点,想着不大,丢到数组里面看有没有空结点。发现不对。。。
后面改了方法,用广度遍历,遍历到空结点的时候,看看是不是所有结点都遍历完了,来判断。
注意输入的时候,不要像我一样用char在读入。数字有两位的。。哎。。。
#include <iostream>#include <cstring>#include <queue>#include <string>#define MAX 25#define null -1using namespace std;struct Node {int left;int right;};Node Tree[MAX];bool isVis[MAX];vector <int> tempNode;int n;int last = 0;bool Check(int root) {queue <int> q;bool isEmpty = false;if (root == null)return true;q.push(root);tempNode.push_back(root);while (!q.empty()) {int temp = q.front();q.pop();last = temp;if (Tree[temp].left == null) {break;}else {q.push(Tree[temp].left);tempNode.push_back(Tree[temp].left);}if (Tree[temp].right == null) {break;}else {q.push(Tree[temp].right);tempNode.push_back(Tree[temp].right);}}if (tempNode.size() != n)return false;return true;}int str2int(string s) {int sum = 0;for (int i = 0; i < s.size(); i++) {sum *= 10;sum += s[i] - '0';}return sum;}int main() {cin >> n;memset(isVis, false, sizeof(isVis));string temp1, temp2;for (int i = 0; i < n; i++) {cin >> temp1 >> temp2;//cout << temp1 << temp2 << endl;if (temp1[0] == '-')Tree[i].left = null;else {Tree[i].left = str2int(temp1);isVis[Tree[i].left] = true;}if (temp2[0] == '-')Tree[i].right = null;else {Tree[i].right = str2int(temp2);isVis[Tree[i].right] = true;}}int root = null;for (int i = 0; i < n; i++) {if (!isVis[i]) {root = i;break;}}bool isCT = Check(root);if (isCT)cout << "YES " << tempNode[n-1] << endl;elsecout << "NO " << root << endl;return 0;}
0 0
- 1110. Complete Binary Tree 解析
- 1110. Complete Binary Tree
- 1110. Complete Binary Tree
- 1110. Complete Binary Tree
- 1110. Complete Binary Tree (25)
- 1110. Complete Binary Tree (25)
- 1110. Complete Binary Tree (25)
- 1110. Complete Binary Tree (25)
- PAT--1110. Complete Binary Tree
- 1110. Complete Binary Tree (25)
- 1110. Complete Binary Tree (25)
- 1110. Complete Binary Tree (25)
- 1110. Complete Binary Tree (25)
- 1110. Complete Binary Tree (25)
- 【PAT】1110. Complete Binary Tree
- 1110. Complete Binary Tree (25)
- (PAT)1110. Complete Binary Tree
- 1110. Complete Binary Tree (25)
- Java设计模式--单例设计模式
- Win10安装之路
- Myeclipse项目通过built Path添加Jar包,运行仍然提示没有**.jar
- Web字体 ---- @font-face
- 定制排序与自然排序
- 1110. Complete Binary Tree 解析
- 剑指Offer面试题29数组中出现次数超过一半的数字(涉及快排)
- php关于<<< 界定符 的用法
- 【win向】Github上文件传输/修改/删除的新手教程
- 单链表
- 抛小球
- lintcode(426)恢复IP地址
- MySQL日常总结
- super和this的区别