CareerCup-4.1

Implement a function to check if a tree is balanced For the purposes of this question, 
a balanced tree is defined to be a tree such that no two leaf nodes differ in distance 
from the root by more than one 

答案是两次遍历分别求出最大深度与最小深度,我试着一次遍历求出最大和最小深度

#include <iostream>
using namespace std;
typedef int Data;

struct TreeNode
{
    TreeNode(Data d):data(d){left = right = NULL;};
    Data data;
    TreeNode* left;
    TreeNode* right;
};

class Tree
{
public:
    Tree():root(NULL){};
    TreeNode* root;
    void insert(Data data, TreeNode* node = NULL)
    {
        if(root == NULL)
        {
            root = new TreeNode(data);
            return;
        }
        if(node == NULL) node = root;
        if(data == node->data)
        {
            return;
        } else if(data < node->data) {
            if(node->left == NULL)
                node->left = new TreeNode(data);    
            else
                insert(data, node->left);
        } else {
            if(node->right == NULL)
                node->right = new TreeNode(data);
            else
                insert(data, node->right);
        }
    };
    void inorderPrint(TreeNode* TreeNode)
    {
        if(TreeNode == root)
            cout<<"Print:"<<endl;
        if(TreeNode != NULL)
        {
            inorderPrint(TreeNode->left);
            cout<<TreeNode->data<<" ";
            inorderPrint(TreeNode->right);
        }
        if(TreeNode == root)
            cout<<endl<<"End"<<endl;
    };
    void preorderPrint(TreeNode* TreeNode)
    {
        if(TreeNode == root)
            cout<<"Print:"<<endl;
        if(TreeNode != NULL)
        {
            cout<<TreeNode->data<<" ";
            preorderPrint(TreeNode->left);
            preorderPrint(TreeNode->right);
        }
        if(TreeNode == root)
            cout<<endl<<"End"<<endl;
    };
    void postorderPrint(TreeNode* node)
    {
        if(node == root)
            cout<<"Print:"<<endl;
        if(node != NULL)
        {
            postorderPrint(node->left);
            postorderPrint(node->right);
            cout<<node->data<<" ";
        }
        if(node == root)
            cout<<endl<<"End"<<endl;
    };
    bool isBalanced()
    {
        int min=0 ,max=0;
        calDepth(root, 0, &min, &max);
        cout<<"Min: "<<min<<endl<<"Max: "<<max<<endl;
        if(max - min < 2)
            return true;
        else
            return false;
    };
    void calDepth(TreeNode* node, int depth, int* min, int* max)
    {
        if(node == NULL) return;
        depth++;
        if(node->left == NULL && node->right == NULL)
        {
            if(depth < *min || *min <= 0)
                *min = depth;
            if(depth > *max || *max <= 0)
                *max = depth;
        }
        calDepth(node->left, depth, min, max);
        calDepth(node->right, depth, min, max);
    };
};

int main()
{
    Tree tree;
    tree.insert(5);
    tree.insert(1);
    tree.insert(7);
    tree.insert(4);
    tree.insert(3);
    cout<<boolalpha<<tree.isBalanced();
    system("pause");
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值