104. 二叉树的最大深度

Posted 如有一味绝境,非历十方生死

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了104. 二叉树的最大深度相关的知识,希望对你有一定的参考价值。


 

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7]

    3
   /   9  20
    /     15   7

返回它的最大深度 3 。

 

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int maxDepth(TreeNode* root) {
13         int maxx = 0;
14         if(root != NULL) {
15             maxx++;
16             int left_max = maxDepth(root->left);
17             int right_max = maxDepth(root->right);
18             maxx += max(left_max,right_max);
19         }
20         return maxx;
21     }
22 };

 

以上是关于104. 二叉树的最大深度的主要内容,如果未能解决你的问题,请参考以下文章

java刷题--104二叉树的最大深度

LeetCode第104题—二叉树的最大深度—Python实现

Leetcode题目104.二叉树的最大深度(DFS+BFS简单)

⭐算法入门⭐《二叉树》简单04 —— LeetCode 104. 二叉树的最大深度

[LeetCode] 104. 二叉树的最大深度

Leetcode104. 二叉树的最大深度(dfs)