Trees and binary search trees · Exercise 26 of 100

Build a binary tree

Give each node a value and two links, one for a left child and one for a right child. Start with empty links and attach new nodes on the side you choose. Keeping a link to the root lets you reach any attached node by following child links.

Try it yourself

Start with root 7. Attach 3 on its left and 10 on its right. Attach 5 on the right of 3. Starting at the root, which value do you reach by following the left link and then the right link?

One-step hint

Mark left and right on the arrows. After the first hop from the root, use the right arrow belonging to the node you reached.

Worked solution
  1. The first link takes you from 7 to its left child, 3.
  2. You now follow the right link stored in node 3, which leads to 5.
DSA compiler