面试题39:二叉树的深度

题目描述

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

题目分析

剑指Offer(纪念版)P207

代码实现

//            1
//         /      
//        2        3
//       /         
//      4  5         6
//        /
//       7

int TreeDepth(BinaryTreeNode* pRoot)
{
    if(pRoot == NULL)
        return 0;

    int nLeft = TreeDepth(pRoot->m_pLeft);
    int nRight = TreeDepth(pRoot->m_pRight);

    return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);
}

  

原文地址:https://www.cnblogs.com/xwz0528/p/4896182.html