[tree] getHeight returns faulty height bug fixed

This commit is contained in:
Revone 2023-10-18 17:07:27 +08:00
parent dc2394ae4d
commit 07825744f4

View file

@ -47,10 +47,8 @@ export class TreeNode<V = any> {
}
getHeight() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const beginRoot = this;
let maxDepth = 1;
if (beginRoot) {
let maxDepth = 0;
if (this) {
const bfs = (node: TreeNode<V>, level: number) => {
if (level > maxDepth) {
maxDepth = level;
@ -62,7 +60,7 @@ export class TreeNode<V = any> {
}
}
};
bfs(beginRoot, 1);
bfs(this, 0);
}
return maxDepth;
}