AlgoFrames

Children of index i

Warm-up · Trace the algorithm · about 4 min

A complete tree of 11 nodes is stored in an array, root at index 0. Find the children and the parent of index 4 using only arithmetic.

Example 1

Input:

{"levelOrder":["A","B","C","D","E","F","G","H","I","J","K"],"i":4}

Output:

{"left":9,"right":10,"parent":1}

Index 4 holds E.

Constraints

  • Indices start at 0
  • The tree is complete

Learn the idea first: Binary trees and how they are stored, a visual lesson.

More Binary trees and how they are stored problems