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;
    }
  • 相关阅读:
    Coherence的NameService
    监控Coherence成员的加入和离开集群事件
    Coherence的集群成员的离开和加入机制研究
    Coherence对象压缩以及对象大小计算
    Coherence代理的负载均衡
    Oracle Service Bus Socket Adapter调整的参数
    Coherence生产环境异常定位过程
    Automatic Diagnostic Repository
    docker(三)反正我不喜欢敲命令,daocloud.io管理你的docker,安装远程下载工具aria2 迅雷远程下载 xware
    docker(二)不管怎么样,先跑起来。运行一个samba,运行一个hello world。
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3092069.html
Copyright © 2011-2022 走看看