Algorithm: Traverse binary tree

  sonic0002        2019-03-01 22:49:04       2,513        0    

Preface

There are three ways to traverse a binary tree based on the traversing order.

  • Pre-Order: Root - Left - Right
  • In-Order: Left - Root - Right
  • Post-Order: Left - Right - Root

Take below binary tree as example

The node to be traversed in different order would be

  • Pre-Order: A - B - C - D - E - F
  • In-Order: C - B - D - A - E - F
  • Post-Order: C - D - B - F - E - A

Below are the implementations of different orders for traversing the binary tree.

Pre-Order Traversing

Recursive implementation

Non-recursive implementation

In-Order traversing

Recursive implementation

Non-recursive implementation

Post-Order traversing

Recursive implementation

Reference: https://mp.weixin.qq.com/s/U7nZYrndMx2jdQf6dAxuww

ALGORITHM  BINARY TREE 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

Samsung Galaxy Note 7 starter package