判断一棵二叉树是否是平衡二叉树/求一颗二叉树的镜像

这道题目一共两问,其一如何判断一颗二叉树是否是平衡二叉树?平衡二叉树指任意一父节点左右字数高度差不大于1,就是说<=1即可。高度,自然有一个求属深度的方法,所以有了如下内容:

bool IsBalance()
{
    return _IsBalance(_root);
}

int depth()
{
    return _depth(_root);
}

int _depth(Node* root)
{
    if (root == NULL)
        return 0;
    int leftheight = _depth(root->_left) + 1;
    int rightheight = _depth(root->_right) + 1;
    return leftheight > rightheight ? leftheight : rightheight;
}

bool _IsBalance(Node* root)
{
    if (root == NULL)
        return true;

    int leftheight = _depth(root->_left);
    int rightheight = _depth(root->_right);
    int is_balance = leftheight - rightheight;
    if (is_balance <= 1 && is_balance >= -1)
        return _IsBalance(root->_left) && _IsBalance(root->_right);
    else
        return false;
}

第二问,求一棵树的镜像?也就是说每一个节点映射到对应的相反的位置。

void BinaryMirror1()
{
    _BinaryMirror(_root);
}

void BinaryMirror2()
{
    if (_root == NULL)
        return;
    queue<Node*> q;
    q.push(_root);
    while (!q.empty())
    {
        Node* cur = q.front();
        swap(cur->_left, cur->_right);
        q.pop();
        if (cur->_left)
            q.push(cur->_left);
        if (cur->_right)
            q.push(cur->_right);
    }
}
void _BinaryMirror(Node* root)
{
    if (root != NULL)
    {
        swap(root->_left, root->_right);
        _BinaryMirror(root->_left);
        _BinaryMirror(root->_right);
    }
}

两个问题的完整代码

#define _CRT_SECURE_NO_WARNINGS 10
#include<iostream>
#include<stdlib.h>
#include<queue>
using namespace std;

template<class T>
struct TreeNode{
    T _data;
    TreeNode<T>* _left;
    TreeNode<T>* _right;

    TreeNode(T x = T())
        :_data(x)
        , _left(NULL)
        , _right(NULL)
    {}
};

template<class T>
class Tree{
    typedef TreeNode<T> Node;
public:
    Tree()
        :_root(NULL)
    {}

    Tree(const T* arr, size_t size, const size_t invalued)
    {
        size_t index = 0;
        _CreateTree(_root,arr, size, index, invalued);
    }

    void PrevOrder()
    {
        cout << "InOrder:";
        _PrevOrder(_root);
        cout << endl;
    }

    bool IsBalance()
    {
        return _IsBalance(_root);
    }

    int depth()
    {
        return _depth(_root);
    }

    void BinaryMirror1()
    {
        _BinaryMirror(_root);
    }

    void BinaryMirror2()
    {
        if (_root == NULL)
            return;
        queue<Node*> q;
        q.push(_root);
        while (!q.empty())
        {
            Node* cur = q.front();
            swap(cur->_left, cur->_right);
            q.pop();
            if (cur->_left)
                q.push(cur->_left);
            if (cur->_right)
                q.push(cur->_right);
        }
    }
protected:
    void _CreateTree(Node*& root,const T arr[], size_t size, size_t& index, T invalued)
    {
        if (index < size&&arr[index] != invalued)
        {
            root = new Node(arr[index]);
            _CreateTree(root->_left, arr, size, ++index, invalued);
            _CreateTree(root->_right, arr, size, ++index, invalued);
        }
    }

    void _PrevOrder(Node* root)
    {
        if (root == NULL)
            return;
        cout << root->_data << " ";
        _PrevOrder(root->_left);
        _PrevOrder(root->_right);
    }

    int _depth(Node* root)
    {
        if (root == NULL)
            return 0;
        int leftheight = _depth(root->_left) + 1;
        int rightheight = _depth(root->_right) + 1;
        return leftheight > rightheight ? leftheight : rightheight;
    }

    bool _IsBalance(Node* root)
    {
        if (root == NULL)
            return true;

        int leftheight = _depth(root->_left);
        int rightheight = _depth(root->_right);
        int is_balance = leftheight - rightheight;
        if (is_balance <= 1 && is_balance >= -1)
            return _IsBalance(root->_left) && _IsBalance(root->_right);
        else
            return false;
    }

    void _BinaryMirror(Node* root)
    {
        if (root != NULL)
        {
            swap(root->_left, root->_right);
            _BinaryMirror(root->_left);
            _BinaryMirror(root->_right);
        }
    }
private:
    Node* _root;
};

int main()
{
    int arr[20] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 };
    int size = sizeof(arr) / sizeof(arr[10]);
    Tree<int> tree(arr,10,'#');
    tree.PrevOrder();
    if (int ret = tree.IsBalance())
        cout << "ture" << endl;
    else
        cout << "false" << endl;
    tree.BinaryMirror2();
    tree.PrevOrder();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值