(Node root, ArrayList<Integer> ans)
| 13 | } |
| 14 | |
| 15 | public static void travel(Node root, ArrayList<Integer> ans) |
| 16 | { |
| 17 | if(root==null) return; |
| 18 | |
| 19 | //if there is node on left, go there |
| 20 | if(root.left!=null)travel(root.left,ans); |
| 21 | //add the element to list |
| 22 | ans.add(root.data); |
| 23 | //if there is node on right, go there |
| 24 | if(root.right!=null)travel(root.right,ans); |
| 25 | } |
| 26 | } |
| 27 | //Time Complexity : o(n) |
| 28 | //Space Complexity : o(n) |