题目及测试
package pid543;
/*543. 二叉树的直径
给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。
示例 :
给定二叉树
1
/ \
2 3
/ \
4 5
返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。
注意:两结点之间的路径长度是以它们之间边的数目表示。
*/
public class main {
public static void main(String[] args) {
Object[] x=new Object[]{1,2,3,4,5,null,null};
BinaryTree tree=new BinaryTree(x);
BinaryTree.printTree(tree.root);
test(tree.root);
}
private static void test(TreeNode ito) {
Solution solution = new Solution();
int rtn;
long begin = System.currentTimeMillis();
rtn = solution.diameterOfBinaryTree(ito);//执行程序
long end = System.currentTimeMillis();
System.out.println("rtn=" );
System.out.print(rtn);
System.out.println();
System.out.println("耗时:" + (end - begin) + "ms");
System.out.println("----------