Real world pre/post-order tree traversal examples

后端 未结 2 1874
我寻月下人不归
我寻月下人不归 2021-01-31 21:59

I understand pre-order, in-order, and post-order tree traversal algorithms just fine. (Reference). I understand a few uses: in-order for traversing binary search trees in order

2条回答
  •  故里飘歌
    2021-01-31 22:28

    As Henk Holterman pointed out, destroying a tree using manual memory management usually is a post-order traversal.

    Pseudocode:

    destroy(node) {
      if (node == null) return;
    
      destroy(node.left)
      destroy(node.right)
    
      // Post-order freeing of current node
      free(node)
    }
    

提交回复
热议问题