数据结构---0302List leaves

mac2026-05-03  8

题目:https://pintia.cn/problem-sets/1169812488801005568/problems/1179652789523730433

https://pintia.cn/problem-sets/1169812488801005568/problems/1179652789523730433 #include<iostream> #include<string> #include<queue> #define null -1 using namespace std; struct BiNode { int data; int left; int right; }T[10]; int create(struct BiNode T[]) { int n; cin >> n; char left, right; int root = 0; if (!n) { return null; } else { for (int i = 0;i < n;i++) { T[i].data = i; cin >> left >> right; if (right == '-') { T[i].right = null; } else { T[i].right = right - '0'; root -= T[i].right; } if (left == '-') { T[i].left = null; } else { T[i].left = left - '0'; root -= T[i].left; } root += i; } } return root; } //层序遍历 void ergodic(int root) { queue<struct BiNode> num; int flag = 0; if (root == null) { cout << "-"; return; } num.push(T[root]); while (!num.empty()) { struct BiNode first = num.front(); num.pop(); if (first.left == null&&first.right == null) { if (!flag) { cout << first.data; flag = 1; } else { cout << " "; flag = 1; cout << first.data; } } else { if (!(first.left == null)) { num.push(T[first.left]); } if (!(first.right == null)) { num.push(T[first.right]); } } } } int main() { int R1; R1 = create(T); ergodic(R1); return 0; }
最新回复(0)