-6

在此处输入图像描述

各位大佬,上面那棵树的高度是多少?它的3对吗?

我老师设计的测试用例期望它是2。

这是我用来获取高度的代码;

public int height(TreeNode t){

if (t == null)
return 0;
int heightLeft = height(t.leftChild);
int heightRight = height(t.rightChild)

if( heightLeft > heightRight )
return heightLeft +1;
else
return heightRight +1;
}

你为什么要关闭这个线程?

4

1 回答 1

2

http://en.wikipedia.org/wiki/Binary_tree

树的深度(或高度)是从根到树中最深节点的路径长度。只有一个节点(根)的(有根)树的深度为零。

于 2012-09-11T05:03:54.730 回答