二叉树系列问题——求二叉树中最远节点的距离

求二叉树中最远节点的距离,我们首先要对最远距离有清晰的认识,最远距离分为以下三类情况
这里写图片描述
这里写图片描述
这里写图片描述
因此我们在计算距离时总要与前一次的距离作比较,只有当此次左右距离之和大于max值才更改max的值,我们可以使用后续遍历来计算最远距离,此时时间复杂度为O(N),使用前序遍历也是可以的,但时间复杂度为O(N*N),综合比较我们选择后续遍历。实现方法写在一个搜索树中了。
代码如下:

#pragma once

#include<iostream>
#include<stack>
#include<queue>

using namespace std;

//寻找二叉树中距离最远的两个节点
//使用递归,保存上一次最远距离,和这次节点的左右最长距离比较,取长的距离作为下次递归的长度
//使用后序遍历和前序遍历都可以结局此问题,但是前序遍历时间复杂度为O(N*N),而后序遍历的时间复杂度为O(N),因此选择后续遍历

template<class T>
struct BSNode
{
    int key;
    BSNode* left;
    BSNode* right;
    BSNode* parent;
    BSNode(T value) :
        key(value)
        , left(NULL)
        , right(NULL)
        , parent(NULL)
    {}
};

template<class T>
class BSTree
{
    typedef BSNode<T> Node;
public:
    BSTree() :
        _root(NULL)
    {}
    ~BSTree()
    {}
private:
    int TreeDepth(Node* root)
    {
        if (root == NULL)
            return 0;

        int nLeftDepth = TreeDepth(root->left);
        int nRightDepth = TreeDepth(root->right);

        return (nLeftDepth>nRightDepth) ? (nLeftDepth + 1) : (nRightDepth + 1);
    }
    int RFindLongDistance(Node* root,int &max,int &LeftLen,int &RightLen)
    {
        if (root == NULL)
        {
            return max;
        }
        RFindLongDistance(root->left,max,LeftLen,RightLen);
        RFindLongDistance(root->right,max,LeftLen,RightLen);
        LeftLen = TreeDepth(root->left);
        RightLen = TreeDepth(root->right);
        return max > (LeftLen + RightLen) ? max : LeftLen + RightLen;
    }

public:
    size_t FindLongerDistance()
    {
        if (_root == NULL)
        {
            return 0;
        }
        int MaxLen = 0;
        int LeftLen = 0;
        int RightLen = 0;
        return RFindLongDistance(_root,MaxLen,LeftLen,RightLen) + 1;
    }
public:
    void Insert(T key)
    {
        Node* newnode = new Node(key);
        if (_root == NULL)
        {
            _root = newnode;
        }
        else
        {
            Node* cur = _root;
            Node* parent = NULL;
            while (cur)
            {
                parent = cur;
                if (key > cur->key)
                {
                    cur = cur->right;
                }
                else if (key < cur->key)
                {
                    cur = cur->left;
                }
                else
                {
                    return;
                }
            }
            if (parent->key > key)
            {
                parent->left = newnode;
            }
            else
            {
                parent->right = newnode;
            }
            newnode->parent = parent;
        }
    }
    void Print()
    {
        if (_root == NULL)
        {
            return;
        }
        _Print(_root);
    }
    void _Print(Node* cur)
    {
        if (cur == NULL)
        {
            return;
        }
        else if (cur->left)
        {
            _Print(cur->left);
        }
        cout << cur->key << " ";
        if (cur->right)
        {
            _Print(cur->right);
        }
    }
    Node* Find(int key)
    {
        Node*  cur = _root;
        while (true)
        {
            if (cur->key > key)
            {
                cur = cur->left;
            }
            else if (cur->key < key)
            {
                cur = cur->right;
            }
            else
            {
                return cur;
            }
        }
        return NULL;
    }

private:
    BSNode<T>* _root;
};

二叉树如下:
这里写图片描述
结果如下:
这里写图片描述
二叉树如下:
这里写图片描述
结果如下:
这里写图片描述
二叉树如下:
这里写图片描述
结果如下:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值