int depth(BinaryNode node)
{
int leftHeight,rightHeight;
if(node == null)
return 0;
if(node.left!=null)
leftHeight = depth(node.left);
else
leftHeight = 0;
if(node.right!=null)
rightHeight = depth(node.right);
else
rightHeight = 0;
return leftHeight>rightHeight? leftheight+1:rightHeight;
}
9度oc 题目1350:二叉树的深度
时间限制:1 秒
内存限制:32 兆
-
题目描述:
-
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
-
-
思路:用递归解决。递归基:找到一个叶子节点并判断左右子树高度,因为都为空所以都为0,返回0+1。