1. 程式人生 > >97.二叉樹的最大深度

97.二叉樹的最大深度

root pub 二叉樹 http depth blog src com cnblogs

轉自[LeetCode] Maximum Depth of Binary Tree 二叉樹的最大深度
技術分享圖片

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: The root of binary tree.
     * @return: An integer
     */
    int maxDepth(TreeNode * root) {
        if (!root) return 0;
        return 1 + max(maxDepth(root->left), maxDepth(root->right));
    }
};

97.二叉樹的最大深度