AcWing 71 二叉树的深度

题目描述:

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

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

样例

输入:二叉树[8, 12, 2, null, null, 6, 4, null, null, null, null]如下图所示:
    8
   / \
  12  2
     / \
    6   4

输出:3

分析:

写求树深度的递归函数。边界情况,访问到空节点,深度为0.一般情况,某结点深度等于左右子树深度最大值加上1.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int treeDepth(TreeNode* root) {
        if(!root)   return 0;
        return max(treeDepth(root->left),treeDepth(root->right)) + 1;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_30277239/article/details/88555633