* Helper method to check descendants * Time O(n) | Space O(n) * @param {number} index * @return {boolean}
(index)
| 105 | * @return {boolean} |
| 106 | */ |
| 107 | checkDescendants(index) { |
| 108 | const stack = []; |
| 109 | stack.push(index); |
| 110 | while (stack.length) { |
| 111 | const node = stack.pop(); |
| 112 | const children = this.childHash[node]; |
| 113 | for (let i = 0; i < (children && children.length); i++) { |
| 114 | if (this.treeHash[children[i]]) return true; |
| 115 | stack.push(children[i]); |
| 116 | } |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | } |