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.
public class Solution {
public int maxDepth(TreeNode root) {
int depth = 0;
if(root != null){
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
depth ++;
if(leftDepth < rightDepth){
depth = depth + rightDepth;
}else{
depth = depth + leftDepth;
}
}
return depth;
}
}
點擊查看更多內容
1人點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦