二元树的深度 【微软面试100题 第五十二题】

题目要求:

  输入一颗二叉树的根结点,求该树的深度。

  从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

  如     3

    /    

   4

    

     2,深度为3

  参考链接:剑指offer第39题。

题目分析:

  用递归的方式从根结点开始,遍历其左右结点,较大值则为该树的深度。

代码实现:

#include <iostream>
#include <stack>

using namespace std;

typedef struct BinaryTree
{
    struct BinaryTree *left,*right;
    int data;
}BinaryTree;

void initTree(BinaryTree **p);
int FindTreeDepth(BinaryTree *root);

int main(void)
{
    BinaryTree *root;
    initTree(&root);
    cout << "二叉树深度为:" << FindTreeDepth(root) << endl;
    return 0;
}
int FindTreeDepth(BinaryTree *root)
{
    if(root==NULL)
        return 0;
    int left = FindTreeDepth(root->left);
    int right = FindTreeDepth(root->right);
    return (left>right) ? (left+1):(right+1);
}
//      10
//     / 
//    5   12
//   / 
//  4   7
void initTree(BinaryTree **p)
{
    *p = new BinaryTree;
    (*p)->data = 10;
 
    BinaryTree *tmpNode = new BinaryTree;
    tmpNode->data = 5;
    (*p)->left = tmpNode;
 
    tmpNode = new BinaryTree;
    tmpNode->data = 12;
    (*p)->right = tmpNode;
    tmpNode->left = NULL;
    tmpNode->right = NULL;
 
    BinaryTree *currentNode = (*p)->left;
 
    tmpNode = new BinaryTree;
    tmpNode->data = 4;
    currentNode->left = tmpNode;
    tmpNode->left = NULL;
    tmpNode->right = NULL;
 
    tmpNode = new BinaryTree;
    tmpNode->data = 7;
    currentNode->right = tmpNode;
    tmpNode->left = NULL;
    tmpNode->right = NULL;
    
    cout << "二叉树为:" <<endl;
    cout << "     " << 10<<endl;
    cout << "    " <<"/" << "  "<< "\" <<endl;
    cout << "   " << 5 << "    " << 12 << endl;
    cout << " " <<"/" << "  "<< "\" <<endl;
    cout << 4 << "    " << 7 << endl;
}

题目扩展:

  输入一颗二叉树的根结点,判断该树是不是平衡二叉树。如果某二叉树中任意结点的左右子树的深度相差不超过1,那么它就是一颗平衡二叉树。 

  如    3

    /  

    4

    

     2,不是平衡二叉树,因为3的左子树深度为2,右子树深度为0,差超过1

  如    3

    /  

    4     5

    

      2 ,是平衡二叉树,因为3的左子树深度为2,右子树深度为1,差不超过1;同理4、5、2的左右子树深度都不超过1,则是平衡。

  参考剑指offer第39题。

题目分析:

  用后序遍历的方式遍历二叉树的每个结点(左+右+中),在遍历到一个结点前我们就已经遍历了它的左右子树。只要在遍历每个结点的时候记录它的深度,我们就可以一边遍历一边判断每个结点是不是平衡的。

代码实现:

  

#include <iostream>
#include <stack>

using namespace std;

typedef struct BinaryTree
{
    struct BinaryTree *left,*right;
    int data;
}BinaryTree;

void initTree(BinaryTree **p);
bool IsBalanced(BinaryTree *root);

int main(void)
{
    BinaryTree *root;
    initTree(&root);
    int isBalanced = IsBalanced(root);
    if(isBalanced)
        cout << "是平衡树" << endl;
    else
        cout << "不是平衡树" << endl;
    return 0;
}
bool IsBalanced(BinaryTree *root,int *depth)
{
    if(root==NULL)
    {
        *depth = 0;
        return true;
    }
    int left,right;
    if(IsBalanced(root->left,&left) && IsBalanced(root->right,&right))
    {
        int dif = left-right;
        if(dif<=1 && dif>=-1)
        {
            *depth = 1+(left>right?left:right);
            return true;
        }
    }
    return false;
}
bool IsBalanced(BinaryTree *root)
{
    int depth = 0;
    return IsBalanced(root,&depth);
}
//      10
//     / 
//    5   12
//   / 
//  4   7
void initTree(BinaryTree **p)
{
    *p = new BinaryTree;
    (*p)->data = 10;
    (*p)->right = NULL;
 
    BinaryTree *tmpNode = new BinaryTree;
    tmpNode->data = 5;
    (*p)->left = tmpNode;
    //加上这里就是平衡树,不加就是上图中去掉了12
 /*   tmpNode = new BinaryTree;
    tmpNode->data = 12;
    (*p)->right = tmpNode;
    tmpNode->left = NULL;
    tmpNode->right = NULL;
 */
    BinaryTree *currentNode = (*p)->left;
 
    tmpNode = new BinaryTree;
    tmpNode->data = 4;
    currentNode->left = tmpNode;
    tmpNode->left = NULL;
    tmpNode->right = NULL;
 
    tmpNode = new BinaryTree;
    tmpNode->data = 7;
    currentNode->right = tmpNode;
    tmpNode->left = NULL;
    tmpNode->right = NULL;
    
    cout << "二叉树为:" <<endl;
    cout << "     " << 10<<endl;
    cout << "    " <<"/" << "  "<< "\" <<endl;
    cout << "   " << 5 << "    " << 12 << endl;
    cout << " " <<"/" << "  "<< "\" <<endl;
    cout << 4 << "    " << 7 << endl;
}
原文地址:https://www.cnblogs.com/tractorman/p/4089308.html