面试题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);
}

  

posted @ 2015-10-20 21:39  枯桃  阅读(136)  评论(0编辑  收藏  举报