(TreeNode node, int currentPath)
| 33 | |
| 34 | // dfs method |
| 35 | public int findLeafNodes(TreeNode node, int currentPath){ |
| 36 | // base case, if no node then return 0 |
| 37 | if(node==null){ |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | // add the current node value to the currentPath (move decimal to right by 1 and add) |
| 42 | currentPath = (currentPath * 10) + node.val; |
| 43 | |
| 44 | // if we are at a non-null node, check if it is a leaf |
| 45 | if(node.left==null && node.right==null){ |
| 46 | // return the solution |
| 47 | return currentPath; |
| 48 | } |
| 49 | |
| 50 | // check find the leaf nodes on the left and right |
| 51 | return findLeafNodes(node.left, currentPath) + findLeafNodes(node.right, currentPath); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // solution using strings |
no test coverage detected