【发布时间】:2020-07-19 16:36:30
【问题描述】:
节点的深度是从根到该节点的边数,对吗?但是如何通过java中的findDepth(Node node)之类的方法找到呢?
【问题讨论】:
-
这能回答你的问题吗? Finding height in Binary Search Tree
标签: java data-structures tree binary-tree
节点的深度是从根到该节点的边数,对吗?但是如何通过java中的findDepth(Node node)之类的方法找到呢?
【问题讨论】:
标签: java data-structures tree binary-tree
这是一个关于递归的简单练习。递归地实现它。
findDepth(Node node)
在findDepth 函数/方法的代码中执行以下操作:
1) 如果节点有父节点返回1 + findDepth(parent)
2)如果节点没有父节点(即是根节点)返回0
【讨论】:
所以,根据this 的回答,已经在stackOverflow 上,您可以这样做:
int findDepth(Node node) {
if (aNode == null) {
return -1;
}
int lefth = findHeight(node.left);
int righth = findHeight(node.right);
if (lefth > righth) {
return lefth + 1;
} else {
return righth + 1;
}
}
【讨论】: