PAT A1110 Complete Binary Tree (25分)

1. 题目

判断是否完全二叉树

2. 代码

  • 因为index可能有两位数,不能使用char c,应该使用string或者char c[3]。建议使用string比较方便。但是不管哪种都要吸收换行符。
  • 根据是否有父节点判断是否是根结点,这点很常用

1. 使用层序遍历

#include <iostream>
#include <string>
#include <queue>
using namespace std;
const int maxn = 30;
struct node {
  int lchild, rchild;
} Node[maxn];
int N;

bool have_father[maxn] = {false};

int cnt = 1, last = -1;

void layerOrder(int root) {   //完全二叉树左孩子结点 2*i
  queue<int> q;
  q.push(root);
  last = root;
  while (!q.empty()) {
    int front = q.front();
    q.pop();
    if (Node[front].lchild != -1) {
      cnt++;
      last = Node[front].lchild;
      q.push(Node[front].lchild);
    } else break;
    if (Node[front].rchild != -1) {
      cnt++;
      last = Node[front].rchild;
      q.push(Node[front].rchild);
    } else break;
  }
  if (cnt == N) printf("YES %d\n", last);
  else printf("NO %d\n", root);
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {   //编号从0开始
    cin.ignore();
    string str1, str2;
    cin >> str1 >> str2;
    if (str1 != "-") {    //如果有左孩子
      Node[i].lchild = stoi(str1);
      have_father[stoi(str1)] = true;
    } else {
      Node[i].lchild = -1;
    }

    if (str2 != "-") {    //如果有右孩子
      Node[i].rchild = stoi(str2);
      have_father[stoi(str2)] = true;
    } else {
      Node[i].rchild = -1;
    }

  }
  int root=-1;
  for (int i = 0; i < N; ++i) {   //没有父结点的就是根结点
    if (!have_father[i]) {
      root = i;
      break;
    }
  }
  layerOrder(root);
  return 0;
}

2. 使用DFS

  • 这里搬运了柳神代码
#include <iostream>
#include <string>
using namespace std;
struct node {
  int lchild, rchild;
} Node[1010];

int maxn = -1, ans;

void DFS(int root, int index) {
  if (index > maxn) {
    maxn = index;
    ans = root;
  }
  if (Node[root].lchild != -1) DFS(Node[root].lchild, index * 2);
  if (Node[root].rchild != -1) DFS(Node[root].rchild, index * 2 + 1);
}

int main() {
  int n, root = 0;
  bool have_child[1010] = {false};
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    string lchild, rchild;
    cin >> lchild >> rchild;
    if (lchild == "-") Node[i].lchild = -1;
    else {
      Node[i].lchild = stoi(lchild);
      have_child[stoi(lchild)] = true;
    }
    if (rchild == "-") Node[i].rchild = -1;
    else {
      Node[i].rchild = stoi(rchild);
      have_child[stoi(rchild)] = true;
    }
  }

  while (have_child[root]) root++;
  DFS(root, 1);
  if (maxn == n) printf("YES %d", ans);
  else printf("NO %d", root);
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是用C语言实现的代码,判断一棵二叉树是否为完全二叉树。 ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; typedef struct Queue { TreeNode **data; int front; int rear; int size; } Queue; Queue *createQueue(int size) { Queue *q = (Queue *)malloc(sizeof(Queue)); q->data = (TreeNode **)malloc(sizeof(TreeNode *) * size); q->front = q->rear = 0; q->size = size; return q; } bool isEmpty(Queue *q) { return q->front == q->rear; } bool isFull(Queue *q) { return (q->rear + 1) % q->size == q->front; } void enqueue(Queue *q, TreeNode *node) { if (isFull(q)) { return; } q->data[q->rear] = node; q->rear = (q->rear + 1) % q->size; } TreeNode *dequeue(Queue *q) { if (isEmpty(q)) { return NULL; } TreeNode *node = q->data[q->front]; q->front = (q->front + 1) % q->size; return node; } bool isCompleteTree(TreeNode* root) { if (root == NULL) { return true; } Queue *q = createQueue(1000); bool flag = false; enqueue(q, root); while (!isEmpty(q)) { TreeNode *node = dequeue(q); if (node->left) { if (flag) { return false; } enqueue(q, node->left); } else { flag = true; } if (node->right) { if (flag) { return false; } enqueue(q, node->right); } else { flag = true; } } return true; } int main() { TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode)); root->val = 1; root->left = (TreeNode *)malloc(sizeof(TreeNode)); root->left->val = 2; root->right = (TreeNode *)malloc(sizeof(TreeNode)); root->right->val = 3; root->left->left = (TreeNode *)malloc(sizeof(TreeNode)); root->left->left->val = 4; root->left->right = (TreeNode *)malloc(sizeof(TreeNode)); root->left->right->val = 5; root->right->left = (TreeNode *)malloc(sizeof(TreeNode)); root->right->left->val = 6; printf("%s\n", isCompleteTree(root) ? "true" : "false"); return 0; } ``` 代码中使用了队列来存储二叉树中的节点,判断是否为完全二叉树的方法是,从根节点开始,每层的节点必须都存在,否则后面的节点都必须是叶子节点才满足完全二叉树的定义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值