求二叉树任意两点间的距离

  输入二叉树两个节点的值,求解这两个节点间的距离

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

typedef struct node
{
    int data;
    node *lchild;
    node *rchild;
}TreeNode;

int node1, node2;
int length = 0;
int flag = 0;

bool FindNode(TreeNode *pRoot)
{
    if (pRoot == NULL)
    {
        return false;
    }
    if (pRoot->lchild != NULL)
    {
        if (FindNode(pRoot->lchild))
        {
            return true;
        }
    }
    if (pRoot->rchild != NULL)
    {
        if (FindNode(pRoot->rchild))
        {
            return true;
        }
    }
    if (pRoot->data == node1 || pRoot->data == node2)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int GetPathLength(TreeNode *pRoot)
{
    int nLen = 0;
    TreeNode *Stack[100];
    int top = -1;
    int flag1;
    TreeNode *p = pRoot, *q;
    do 
    {
        while(p != NULL)
        {
            Stack[++top] = p;
            p = p->lchild;
        }
        q = NULL;
        flag1 = 1;
        while(top >= 0 && flag1)
        {
            p = Stack[top];
            if (p->rchild == q)
            {
                if (p->data == node1 || p->data == node2)
                {
                    for (int i = 0; i <= top; i++)
                    {
                        ++nLen;
                    }
                    return nLen;
                }
                --top;
                q = p;
            }
            else
            {
                p = p->rchild;
                flag1 = 0;
            }
        }
    } while (top >= 0);
}

void GetLength(TreeNode *pRoot)
{
    if (pRoot == NULL)
    {
        return;
    }
    if (flag == 1)
    {
        return;
    }
    if ((pRoot->data == node1 || pRoot->data == node2) && FindNode(pRoot->lchild))
    {
        length = GetPathLength(pRoot->lchild);
        flag = 1;
        return;
    }
    if ((pRoot->data == node1 || pRoot->data == node2) && FindNode(pRoot->rchild))
    {
        length = GetPathLength(pRoot->rchild);
        flag = 1;
        return;
    }
    if (FindNode(pRoot->lchild) && FindNode(pRoot->rchild))
    {
        length = GetPathLength(pRoot->lchild) + GetPathLength(pRoot->rchild);
        flag = 1;
        return;
    }
    if (pRoot->lchild != NULL)
    {
        GetLength(pRoot->lchild);
    }
    if (pRoot->rchild != NULL)
    {
        GetLength(pRoot->rchild);
    }
}

void CreateTree(TreeNode *&pRoot, int data)
{
    if (pRoot == NULL)
    {
        pRoot = (TreeNode *)malloc(sizeof(TreeNode));
        pRoot->data = data;
        pRoot->lchild = pRoot->rchild = NULL;
        return;
    }
    if (data < pRoot->data)
    {
        CreateTree(pRoot->lchild, data);
    }
    else
    {
        CreateTree(pRoot->rchild, data);
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    TreeNode *pRoot = NULL;
    int arr[100];
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }
    for (int i = 0; i < n; i++)
    {
        CreateTree(pRoot, arr[i]);
    }
    scanf("%d%d", &node1, &node2);
    GetLength(pRoot);
    printf("%d\n", length);
    return 0;
}
原文地址:https://www.cnblogs.com/lzmfywz/p/3092069.html