insert nodes into a binary search tree
(root *node, val int)
| 21 | |
| 22 | // insert nodes into a binary search tree |
| 23 | func insert(root *node, val int) *node { |
| 24 | if root == nil { |
| 25 | return newNode(val) |
| 26 | } |
| 27 | if val < root.val { |
| 28 | root.left = insert(root.left, val) |
| 29 | } else { |
| 30 | root.right = insert(root.right, val) |
| 31 | } |
| 32 | return root |
| 33 | } |
| 34 | |
| 35 | // inorder traversal algorithm |
| 36 | // Copies the elements of the bst to the array in sorted order |