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;
    }
  • 相关阅读:
    SSH2练习中问题之org.apache.jasper.JasperException: /findAllProductions_list.jsp(31,1) TLD又はタグファイル中のattribute指示子に従って属性itemsはどんな式も受け付けません解决方案
    关于HibernateTempleate模版很多代码可以直接使用,是开发人员不可多得选择
    Java实现权限控制之2
    struts 之 constant
    JSTL表达式之<c:forEach>
    关于事后检查和确认
    关于删除
    jcommander 解析boolean参数
    多线程
    关于proto文件的更新
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3092069.html
Copyright © 2011-2022 走看看