當(dāng)前位置:
首頁(yè) >
【leetcode】104. Maximum Depth of Binary Tree
發(fā)布時(shí)間:2023/12/19
37
豆豆
生活随笔
收集整理的這篇文章主要介紹了
【leetcode】104. Maximum Depth of Binary Tree
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 題目
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
2. 思路
遞歸到左、右子樹的max值,+1.
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));} };總結(jié)
以上是生活随笔為你收集整理的【leetcode】104. Maximum Depth of Binary Tree的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java 监听客户端的退出_Java S
- 下一篇: oracle logminer java