104. Maximum Depth of Binary Tree(圖解)
104. Maximum Depth of Binary Tree
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.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its depth = 3.
Solution
C++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root) return 0;
return 1 + max(maxDepth(root->left),maxDepth(root->right));
}
};
Explanation
相關文章
- LeetCode 104. Maximum Depth of Binary TreeLeetCode
- 104. Maximum Depth of Binary Tree--LeetCode RecordLeetCode
- Leetcode Maximum Depth of Binary TreeLeetCode
- [leetcode]maximum-depth-of-binary-treeLeetCode
- Leetcode-Maximum Depth of Binary TreeLeetCode
- Maximum Depth of Binary Tree leetcode javaLeetCodeJava
- Maximum Depth of Binary Tree 二叉樹的深度二叉樹
- 【LeetCode從零單排】No104 Maximum Depth of Binary TreeLeetCode
- 【LeetCode 104_二叉樹_遍歷】Maximum Depth of Binary TreeLeetCode二叉樹
- Leetcode Minimum Depth of Binary TreeLeetCode
- Leetcode-Minimum Depth of Binary TreeLeetCode
- Minimum Depth of Binary Tree leetcode javaLeetCodeJava
- Leetcode Binary Tree Maximum Path SumLeetCode
- LeetCode のminimum-depth-of-binary-treeLeetCode
- Binary Tree Maximum Path Sum leetcode javaLeetCodeJava
- leetcode-124-Binary Tree Maximum Path SumLeetCode
- LeetCode 124. Binary Tree Maximum Path SumLeetCode
- 【LeetCode 111_二叉樹_遍歷】Minimum Depth of Binary TreeLeetCode二叉樹
- [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉樹的各層建立連結串列二叉樹
- Leetcode Binary Tree PathsLeetCode
- 173. Binary Search Tree Iterator
- LeetCode Invert Binary TreeLeetCode
- Leetcode Balanced Binary TreeLeetCode
- LeetCode 272 Closest Binary Tree Traversal II 解題思路LeetCode
- Construct String from Binary TreeStruct
- [LintCode] Binary Tree Level Order
- [LintCode] Check Full Binary Tree
- LeetCode-Binary Tree PathsLeetCode
- leetcode - Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Inorder TraversalLeetCode
- Leetcode Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Postorder TraversalLeetCode
- Leetcode-Balanced Binary TreeLeetCode
- Balanced Binary Tree leetcode javaLeetCodeJava
- Leetcode-Bianry Tree Maximum Path SumLeetCode
- LeetCode545.Boundary-of-Binary-TreeLeetCode
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode