ALL
Algorithm: Traverse binary tree
PrefaceThere are three ways to traverse a binary tree based on the traversing order.Pre-Order: Root - Left - RightIn-Order: Left - Root - RightPost-Order: Left - Right - RootTake below binary tree as exampleThe node to be traversed in different order would bePre-Order: A - B - C - D - E - FIn-Order: C - B - D - A - E - FPost-Order: C - D - B - F - E - ABelow are the implementations of different orders for traversing the binary tree.Pre-Order TraversingRecursive implementationNon-recursive implementationIn-Order traversingRecursive implementationNon-recursive implementationPost-Order traversing...
2,514 0 ALGORITHM BINARY TREE
Binary tree iterator algorithm
Binary tree pre-order,in-order and post-order traversal are basics in algorithm and data structure.And the recursive binary tree traversal is a classical application of recursion.We define a binary tree node as :// C++struct Node { int value; Node *left; Node *right;}In order binary tree traversal can be:// C++void inorder_traverse(Node *node) { if (NULL != node->left) { inorder_traverse(node->left); } do_something(node); if (NULL != node->right) { inorder_traverse(node->right); }}Easy enough, right? Pre-order and post-order traversal algorithm...
11,882 1 ITERATOR BINARY TREE TRAVERSAL