Merge pull request #9 from zrwusa/tree

[tree] getHeight returns faulty height bug fixed
This commit is contained in:
zrwusa 2023-10-18 17:07:49 +08:00 committed by GitHub
commit 6d560f4842
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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;
}