首页 > 其他 > 详细

LeetCode104二叉树的最大深度

时间:2020-07-25 10:21:26      阅读:77      评论:0      收藏:0      [点我收藏+]

题目链接

https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/

题解

  • 递归
  • 递归出口是当前结点为空,则返回0
  • 如果非空,则该结点深度为左子树和右子树深度的最大值+1
// Problem: LeetCode 104
// URL: https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/
// Tags: Tree Recursion
// Difficulty: Easy

#include <iostream>
#include <algorithm>
using namespace std;

struct TreeNode{
    TreeNode* left;
    TreeNode* right;
    int val;
    TreeNode(int x):val(x), left(nullptr), right(nullptr){}
};

class Solution{
public:
    int maxDepth(TreeNode* root){
        if(nullptr==root)
            return 0;
        return max(maxDepth(root->left), maxDepth(root->right))+1;
    }
};

int main()
{
    cout << "helloworld" << endl;
    // system("pause");
    return 0;
}

作者:@臭咸鱼

转载请注明出处:https://www.cnblogs.com/chouxianyu/

欢迎讨论和交流!


LeetCode104二叉树的最大深度

原文:https://www.cnblogs.com/chouxianyu/p/13374983.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!