(tree, rootValue)
| 6 | |
| 7 | // traverses a give tree from specified root's value |
| 8 | function traverseDFS(tree, rootValue) { |
| 9 | const stack = [] |
| 10 | const res = [] |
| 11 | stack.push(searchDFS(tree, rootValue)) |
| 12 | // if root is not present in the tree, returning empty array |
| 13 | if (!stack[0]) return res |
| 14 | while (stack.length) { |
| 15 | const curr = stack.pop() |
| 16 | res.push(curr.value) |
| 17 | if (curr.left) { |
| 18 | stack.push(tree[curr.left]) |
| 19 | } |
| 20 | if (curr.right) { |
| 21 | stack.push(tree[curr.right]) |
| 22 | } |
| 23 | } |
| 24 | return res.reverse() |
| 25 | } |
| 26 | |
| 27 | function searchDFS(tree, value) { |
| 28 | const stack = [] |
no test coverage detected