zoukankan      html  css  js  c++  java
  • 求二叉树任意两点间的距离

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

    #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;
    }
  • 相关阅读:
    数据终端设备与无线通信模块之间串行通信链路复用协议(TS27.010)在嵌入式系统上的开发【转】
    设备树网址【原创笔记】
    clock()、time()、clock_gettime()和gettimeofday()函数的用法和区别【转】
    ajaxFileUpload SyntaxError: syntax error
    工厂模式
    程序猿都是project师吗?
    [android开发之内容更新类APP]二、这几日的结果
    Java实现将指定目录内的指定类型的文件归类
    移动支付之智能IC卡与Android手机进行NFC通信
    Java并发框架——AQS堵塞队列管理(一)——自旋锁
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3092069.html
Copyright © 2011-2022 走看看