cxfans
9/4/2019 - 1:36 AM

p169-7

p169-7

#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>

#define ElemType int

typedef struct BiTNode {
    ElemType data;
    struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;

int GetLayer(BiTree t, BiTree p, int depth) {
    if (t) {
        if (t == p) {
            printf("%d", depth);
            exit(0);
        };
        if (p->data < t->data) GetLayer(t->lchild, p, depth + 1);
        else GetLayer(t->rchild, p, depth + 1);
    }
    return 0;
}

void func(BiTree t, BiTree p) {
    GetLayer(t, p, 1);
}

int level(BiTree t, BiTree p) {
    int count = 0;
    if (t) {
        count++;
        while (t->data != p->data) {
            if (p->data < t->data) t = t->lchild;
            else t = t->rchild;
            count++;
        }
    }
    return count;
}