Mark As Completed Discussion

Good morning! Here's our prompt for today.

Can you write a function to traverse a binary tree in-order, and print out the value of each node as it passes?

SNIPPET
1  4
2   \
3    5
4   /
5  6

The example would output [4, 6, 5] since there is no left child for 4, and 6 is visited in-order before 5.

Description

The definition of a tree node is as follows:

1function Node(val) {
2  this.val = val;
3  this.left = null;
4  this.right = null;
5}

Follow up: you'll likely get the recursive solution first, could you do it iteratively?

Constraints

  • Number of vertices in the tree <= 100000
  • The values of the vertices in the tree will be between -1000000000 and 1000000000
  • Expected time complexity : O(n)
  • Expected space complexity : O(1)

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Tired of reading? Watch this video explanation!

To change the speed of the video or see it in full screen, click the icons to the right of the progress bar.

Here's our guided, illustrated walk-through.

How do I use this guide?

This is one of those questions where knowing the terminology is necessary. In-order traversal visits the left child nodes first, then the root, followed by the right child (remember, it's called in-order because the current node's value is read in-between the left and right child nodes).

Recall the following ordering types:

Inorder traversal

  1. First, visit all the nodes in the left subtree
  2. Then the root node
  3. Visit all the nodes in the right subtree

When to use? With a BST, the in order traversal will give the elements in ascending order, useful for problems where order matters.

SNIPPET
1// pseudo-logic of the inorder function:
2inorder(root.left)
3display(root.data)
4inorder(root.right)
Step Two
JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Preorder traversal

  1. Visit root node
  2. Then go to all the nodes in the left subtree
  3. Visit all the nodes in the right subtree

When to use? You can use pre-order traversal to create a copy of the tree, since you can access each node before its subtrees. It's also used to get expressions on an expression tree.

SNIPPET
1display(root.data)
2preorder(root.left)
3preorder(root.right)
Step Three

Note: This algorithm is equivalent to the famous graph algorithm Depth First Search (DFS).

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Postorder traversal

  1. Visit all the nodes in the left subtree
  2. Visit all the nodes in the right subtree
  3. Visit the root node

When to use? You can use post-order traversal to delete a tree, since you can delete all of a node's children before itself.

SNIPPET
1postorder(root.left)
2postorder(root.right)
3display(root.data)
Step Four
JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Complexity Analysis

Worst case scenario time complexity is O(n). Where n is the number of nodes in the tree (in the worst case, we'd need to go through every node to find the value we want).

As this is the recursive solution, it will require memory in the stack for each call of the traversal functions. So on average (balanced binary tree), the space complexity is O(logn). And in the worst case (skewed binary tree), the space complexity will be O(n).

For more information on tree complexities, check out our Big O Notation Cheat Sheet.

Note: Will the space complexity be same if we use an iterative solution for the traversal? Tell us in the discussion!

One Pager Cheat Sheet

  • Write a function to traverse a binary tree in-order and print the value of each node while it passes, while meeting the specified complexity requirements.
  • In-order traversal visits the left child nodes first, followed by the root, and then the right child, and is useful for obtaining ascending sorted order inBSTs.
  • Preorder traversal can be used to create a copy of the tree or to get expressions from an expression tree (equivalent to Depth First Search (DFS)) by displaying the root's data and then traversing its left and right subtrees.
  • With post-order traversal, display(root.data) will be called after traversing both left and right subtrees of the root node, which is useful for certain operations like tree deletion.
  • The worst case time complexity for a recursive tree traversal is O(n), and the corresponding space complexity is on average O(logn) (balanced binary tree) and O(n) (skewed binary tree).

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

You're doing a wonderful job. Keep going!

If you had any problems with this tutorial, check out the main forum thread here.