(String traversal, int depth)
| 23 | return recur(traversal,0); |
| 24 | } |
| 25 | public TreeNode recur(String traversal, int depth){ |
| 26 | if(index >= n) return null; |
| 27 | //count the dash |
| 28 | int count=0; |
| 29 | int tempI=index; |
| 30 | while(tempI < n && !Character.isDigit(traversal.charAt(tempI))){ |
| 31 | count++; |
| 32 | tempI++; |
| 33 | } |
| 34 | if(count!=depth) return null; |
| 35 | index = tempI; |
| 36 | //find the number |
| 37 | int val=0; |
| 38 | while(index < n && Character.isDigit(traversal.charAt(index))){ |
| 39 | val = val * 10 + (traversal.charAt(index) - '0'); |
| 40 | index++; |
| 41 | } |
| 42 | TreeNode node = new TreeNode(val); |
| 43 | node.left = recur(traversal, depth+1); |
| 44 | node.right = recur(traversal, depth+1); |
| 45 | return node; |
| 46 | } |
| 47 | |
| 48 | } |
| 49 |
no test coverage detected