?LeetCode刷題實戰(zhàn)104:二叉樹的最大深度
算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !
今天和大家聊的問題叫做?二叉樹的最大深度,我們先來看題面:
https://leetcode-cn.com/problems/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.
題意

解題
class?Solution?{
????public?int?maxDepth(TreeNode root) {
????????if?(root == null) {
????????????return?0;
????????}
????????return?Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
????}
}
上期推文:
評論
圖片
表情
