Data Structures & AlgorithmsEasyInfosys · Capgemini pattern

Which traversal should be used to free every node of a binary tree, so that no node is freed before its children?

  1. A. Preorder
  2. B. Inorder
  3. C. Postorder
  4. D. Level order

Correct answer: C. Postorder

Postorder visits left subtree, right subtree, then the node, so both children are already freed when the node itself is freed. Preorder frees the parent first and loses the pointers to its children.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic