(TreeNode root, List<Integer> ls)
| 19 | return ls; |
| 20 | } |
| 21 | public void preorder(TreeNode root, List<Integer> ls){ |
| 22 | /* Preorder traversal is visiting : root then left subtree then right subtree */ |
| 23 | |
| 24 | /* if node is null, then return back to the caller function */ |
| 25 | if(root==null) |
| 26 | return; |
| 27 | |
| 28 | /* Add the current node's value to the list */ |
| 29 | ls.add(root.val); |
| 30 | |
| 31 | /* Recurse for left subtree */ |
| 32 | preorder(root.left, ls); |
| 33 | |
| 34 | /* Recurse for right subtree */ |
| 35 | preorder(root.right, ls); |
| 36 | } |
| 37 | } |