leetcode【树】-----104. Maximum Depth of Binary Tree(二叉树的最大深度)

1、题目描述

2、分析

        二叉树的最大深度是一个很典型的深度优先搜索,很典型的递归应用。

3、代码

/**
 * 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 maxDepth(TreeNode* root) {
        if(root==NULL) return 0;
        return 1+max(maxDepth(root->left),maxDepth(root->right));
        
    }
};

4、相关知识点

        二叉树的遍历,递归。

posted @ 2019-04-13 19:57  吾之求索  阅读(75)  评论(0)    收藏  举报