判别二叉树层序遍历递增
#include<bits/stdc++.h> using namespace std; struct node { int val; int left; int right; }; int main() { int n; cin >> n; while(n--) { int k; cin >> k; vector<node> tree(k); vector<int> num(k, 0); //用来找根节点 for(int i = 0;i < k;i++) { cin >> tree[i].val >> tree[i].left >> tree[i].right; if(tree[i].left != -1) num[tree[i].left] = 1; if(tree[i].right != -1) num[tree[i].right] = 1; } int root = -1; for(int i = 0;i < k;i++) { if(num[i] == 0) { root = i; break; } } vector<int> treeSum; queue<node> q; q.push(tree[root]); //第几个是根节点 将其放进去 //层次遍历 while(!q.empty()) { int size = q.size(); int cnt = 0; for(int i = 0;i < size;i++) // { node head = q.front(); q.pop(); cnt += head.val; if(head.left != -1)//不是叶节点 { q.push(tree[head.left]); } if(head.right != -1)//不是叶节点 { q.push(tree[head.right]); } } treeSum.push_back(cnt); //每一层放一次 } bool ans = true; for(int i = 0;i < treeSum.size() - 1;i++) { if(treeSum[i+1] < treeSum[i]) { ans = false; break; } } if(ans) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
看,这样效果是一样的,只不过分层打印了