| 31 | // traversal of the tree is recursive to ensure travel down |
| 32 | // both the right and left children nodes |
| 33 | var traversal = (root, depth) => { |
| 34 | let current = root; |
| 35 | let right; |
| 36 | let left; |
| 37 | if(!current) { |
| 38 | return null; |
| 39 | // this checks if the current node is a leaf node |
| 40 | } if (!current.right && !current.left) { |
| 41 | return depth; |
| 42 | } |
| 43 | right = traversal(current.right, depth+1) |
| 44 | left = traversal(current.left, depth+1) |
| 45 | if(!right) { |
| 46 | return left; |
| 47 | } else if(!left) { |
| 48 | return right; |
| 49 | } else { |
| 50 | if(right < left) { |
| 51 | return right |
| 52 | } else { |
| 53 | return left |
| 54 | } |
| 55 | } |
| 56 | } |