# Prompt

You’re given the of a binary tree and it’s your task to return all the nodes within the binary tree .

# Approach

This problem is testing your knowledge of whether if you know how to code out an traversal algorithm for a Binary Tree. The traversal algorithm for a binary tree is quite straightforward once you’ve seen it. I’ll be writing the traversal algorithm recursively since we’re dealing with a tree and a tree might have more than 1 level in which a recursive algorithm really comes into handy.

For this problem, as we traverse the Binary Tree , we also want to append all the node values into an array so we’ll go ahead and instantiate an empty array.

## Base Case

Our base case is going to be checking if the is null which basically means that during our traversal algorithm, we ended up at a null node or an empty tree. In that scenario, we just want to simply return.

`if not root:  return`

## Recursive Case

An traversal is different from its counterparts ( & ) by having our action (in this case, appending to an array) in between the recursive calls of our left & right subtrees respectively. This is because just like how the name of the traversal algorithm implies (), we want to append to our array as if it’s literally in order or in sorted order.

`inorder(root.left, arr)arr.append(root.val)inorder(root.right, arr)`

# Code

In case if you haven’t yet joined my Discord server where I host daily sessions on data structures & algorithms for free, please join via the link shared below.

Discord

If you have any questions or comments, please don’t be afraid to connect with me on any of the platforms below or feel free to send me an email at cloudiosx@gmail.com

Portfolio

GitHub

--

--

## More from John Kim

iOS Developer | Full Stack Developer | Software Engineer | LinkedIn: john-kim-developer | GitHub: cloudiosx | Portfolio: cloudiosx.com

Love podcasts or audiobooks? Learn on the go with our new app.

## John Kim

75 Followers

iOS Developer | Full Stack Developer | Software Engineer | LinkedIn: john-kim-developer | GitHub: cloudiosx | Portfolio: cloudiosx.com