【发布时间】:2017-03-10 16:16:03
【问题描述】:
我写了一个方法来返回二叉搜索树的高度。
现在我正在尝试从递归方法返回height - 1。我通过添加额外的if 条件来做到这一点。
有没有更好的方法从递归函数中返回value - 1?
static int height(Node root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right==null) {
return 1;
} else
// I want to return height - 1.
// For example if max height is 10, I wanted to return 9.
return (1 + Math.max(height(root.left), height(root.right));
}
}
【问题讨论】:
标签: java recursion binary-search-tree