剑指offer-38-二叉树的深度
二叉树的深度_牛客网
https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b?tpId=13&tqId=11191&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
此题用递归的思想做就非常简单了。
public class Solution {
public int TreeDepth(TreeNode root) {
if(root == null)return 0;
int leftDepth = TreeDepth(root.left);
int rightDepth = TreeDepth(root.right);
int result = 1 + ((leftDepth > rightDepth)?leftDepth:rightDepth);
return result;
}
}
另外一种层次遍历的一种解法:
public int
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
小白刷剑指offer 文章被收录于专栏
跟着小白一起刷剑指offer,通过讨论加深印象吧~ 没有人不学习就能够掌握知识,知识就是需要学习的~
查看4道真题和解析