输出二叉树中随机两个结点的最小公共父结点

来源:互联网 发布:淘宝异常订单处理中心 编辑:程序博客网 时间:2024/05/18 02:11

思路:当遇到一个结点是返回1,当左右子树都返回1的时候,即最小公共父节点。

//二叉树的数据结构typedef struct MyStruct{    char data;    struct MyStruct *leftChild;    struct MyStruct *rightChild;}Node, *Tree;//查找方法int findFirstFather(Tree root, char first, char second,char &destination){    int i, j;    if (root==NULL)    {        return 0;    }    if (root->data == first || root->data == second)    {        return 1;    }    else    {        i = findFirstFather(root->leftChild, first, second, destination);        j = findFirstFather(root->rightChild, first, second, destination);        if (i == 1 && j == 1)        {            destination = root->data;        }        if (i||j)        {            return 1;        }    }    return 0;}输入:ABC##DE#G##F###输出:D
0 0
原创粉丝点击