面试题39:二叉树的深度

/*题目1:输入一棵二叉树的结点,求该树的深度。从根节点到叶节点依次经过的结点(含根、叶结点)形成树的一条路径
,最长路径的长度为树的深度*/
/*题目2:输入一棵二叉树的根结点,判断该树是不是平衡二叉树。如果某二叉树中任意结点的左右子树的深度相差不超过
1,那么它就是一棵平衡二叉树。*/
#include <iostream>
using namespace std;
//定义一个二叉树结点
struct BinaryTreeNode
{
    int m_nValue;
    BinaryTreeNode *m_pLeft;
    BinaryTreeNode *m_pRight;
};
//创建一个二叉树结点
BinaryTreeNode *CreateBinaryTreeNode(int Value)
{
    BinaryTreeNode *pNode = new BinaryTreeNode;
    pNode -> m_nValue = Value;
    pNode -> m_pLeft = NULL;
    pNode -> m_pRight = NULL;
    return pNode;
}
//创建二叉树
void ConnectBinaryTreeNode(BinaryTreeNode *pRoot, BinaryTreeNode *pLeft, BinaryTreeNode *pRight)
{
    pRoot -> m_pLeft = pLeft;
    pRoot -> m_pRight = pRight;
}
//释放二叉树空间
void DestroyBinaryTreeNode(BinaryTreeNode *pRoot)
{
    if(pRoot)
    {
        DestroyBinaryTreeNode(pRoot -> m_pLeft);
        DestroyBinaryTreeNode(pRoot -> m_pRight);
        delete pRoot;
    }
}
//计算二叉树的深度
int BinaryTreeDepth(BinaryTreeNode *pRoot)
{
    if(pRoot == NULL)
    return 0;
    int Left = BinaryTreeDepth(pRoot -> m_pLeft);
    int Right = BinaryTreeDepth(pRoot -> m_pRight);
    return 1 + (Left > Right ? Left : Right);

}
//判断二叉树是不是平衡二叉树
bool IsBalancedBinaryTree(BinaryTreeNode *pRoot, int *pDepth)
{
    //递归退出条件,同时存储对应结点的二叉树深度
    if(pRoot == NULL)
    {
        *pDepth = 0;
        return true;
    }
    int Left = 0;
    int Right = 0;
    //递归判断是否符合平衡二叉树的条件,同时存储二叉树深度(后序遍历二叉树)
    if(IsBalancedBinaryTree(pRoot -> m_pLeft, &Left) && IsBalancedBinaryTree(pRoot -> m_pRight, &Right))
    {
        int dif = Left - Right;
        if(dif <= 1 && -1 <= dif)
        {
            *pDepth = 1 + (Left > Right ? Left : Right);
            return true;
        }
    }
    return false;
}
//重载二叉树判断函数,同时传入相应参数
bool IsBalancedBinaryTree(BinaryTreeNode *pRoot)
{
    int pDepth = 0;
    return IsBalancedBinaryTree(pRoot, &pDepth);
}
//========================测试代码========================
void Test(char *TestName, BinaryTreeNode *pRoot, int ExpectedDepth, bool ExpectedResult)
{
    if(TestName != NULL)
    cout << TestName << " begins:" << endl;

    if(BinaryTreeDepth(pRoot) == ExpectedDepth)
    cout << "Depth Passed!" << endl;
    else
    cout << "Depth Failed!" << endl;

    if(IsBalancedBinaryTree(pRoot) == ExpectedResult)
    cout << "Test Passed!" << endl;
    else
    cout << "Test  Failed!" << endl;
}
//完全二叉树
//                      1
//                    /   \
//                   2     3
//                  / \   / \
//                 4   5  6  7
void Test1()
{
    BinaryTreeNode *pNode1 = CreateBinaryTreeNode(1);
    BinaryTreeNode *pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode *pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode *pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode *pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode *pNode7 = CreateBinaryTreeNode(7);

    ConnectBinaryTreeNode(pNode1, pNode2, pNode3);
    ConnectBinaryTreeNode(pNode2, pNode4, pNode5);
    ConnectBinaryTreeNode(pNode3, pNode6, pNode7);

    Test("Test1", pNode1, 3, true);

    DestroyBinaryTreeNode(pNode1);
}
//不是完全二叉树,但是平衡二叉树
//                      1
//                    /   \
//                   2     3
//                  / \     \
//                 4   5     6
//                    /
//                    7
void Test2()
{
    BinaryTreeNode *pNode1 = CreateBinaryTreeNode(1);
    BinaryTreeNode *pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode *pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode *pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode *pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode *pNode7 = CreateBinaryTreeNode(7);

    ConnectBinaryTreeNode(pNode1, pNode2, pNode3);
    ConnectBinaryTreeNode(pNode2, pNode4, pNode5);
    ConnectBinaryTreeNode(pNode3, NULL, pNode6);
    ConnectBinaryTreeNode(pNode5, pNode7, NULL);

    Test("Test2", pNode1, 4, true);

    DestroyBinaryTreeNode(pNode1);
}
//不是平衡二叉树
//                      1
//                    /   \
//                   2     3
//                  / \
//                 4   5
//                    /
//                    6
void Test3()
{
    BinaryTreeNode *pNode1 = CreateBinaryTreeNode(1);
    BinaryTreeNode *pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode *pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode *pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode *pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode *pNode7 = CreateBinaryTreeNode(7);

    ConnectBinaryTreeNode(pNode1, pNode2, pNode3);
    ConnectBinaryTreeNode(pNode2, pNode4, pNode5);
    ConnectBinaryTreeNode(pNode5, pNode6, NULL);

    Test("Test3", pNode1, 4, false);

    DestroyBinaryTreeNode(pNode1);
}
//任意结点的右子树深度均为1,不是平衡二叉树
//              1
//              /
//             2
//            /
//           3
//          /
//         4
//        /
//       5
void Test4()
{
    BinaryTreeNode *pNode1 = CreateBinaryTreeNode(1);
    BinaryTreeNode *pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode *pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode *pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5);

    ConnectBinaryTreeNode(pNode1, pNode2, NULL);
    ConnectBinaryTreeNode(pNode2, pNode3, NULL);
    ConnectBinaryTreeNode(pNode3, pNode4, NULL);
    ConnectBinaryTreeNode(pNode4, pNode5, NULL);

    Test("Test4", pNode1, 5, false);

    DestroyBinaryTreeNode(pNode1);
}
//任意结点的左子树深度均为1,不是平衡二叉树
// 1
//  \
//   2
//    \
//     3
//      \
//       4
//        \
//         5
void Test5()
{
    BinaryTreeNode *pNode1 = CreateBinaryTreeNode(1);
    BinaryTreeNode *pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode *pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode *pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5);

    ConnectBinaryTreeNode(pNode1, NULL, pNode2);
    ConnectBinaryTreeNode(pNode2, NULL, pNode3);
    ConnectBinaryTreeNode(pNode3, NULL, pNode4);
    ConnectBinaryTreeNode(pNode4, NULL, pNode5);

    Test("Test5", pNode1, 5, false);

    DestroyBinaryTreeNode(pNode1);
}
//树上只有一个结点
void Test6()
{
    BinaryTreeNode *pNode1 = CreateBinaryTreeNode(1);
    Test("Test6", pNode1, 1, true);

    DestroyBinaryTreeNode(pNode1);
}
//输入空树
void Test7()
{
    Test("Test7", NULL, 0, true);
}
int main()
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();
    Test7();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值