Summary of Binary Tree Algorithms

Source: Internet
Author: User

Basic Algorithms for recording Binary Trees

Binary Tree node structure:

typedef struct TreeNode{    int val;    struct TreeNode *left;    struct TreeNode *right;           }TreeNode,*Node;

1. Traverse

Recursive traversal of forward, center, and backward order:

void pre_order_traversal(TreeNode *root){    if(root! = NULL){        visit(root);        pre_order_traversal(root->left);        pre_order_traversal(root->right);    }}
void in_order_traversal(TreeNode *root){    if(root! = NULL){        pre_order_traversal(root->left);        visit(root);        pre_order_traversal(root->right);    }}
void post_order_traversal(TreeNode *root){    if(root! = NULL){        pre_order_traversal(root->left);        pre_order_traversal(root->right);        visit(root);    }}

Non-recursive traversal of forward, center, and backward orders:

void pre_order_traversal(TreeNode *root){    if(root==NULL)return;    stack<TreeNode> stack;    while(root!=NULL || !stack.empty()){        if(root){            visit(root);            stack.push(root);            root=root->left;        }else{            root=stack.pop();            root=root->right;        }    }}
void in_order_traversal(TreeNode *root){    if(root==NULL)return;    stack<TreeNode> stack;    while(root!=NULL || !stack.empty()){        if(root){            stack.push(root);            root=root->left;        }else{            root=stack.pop();            visit(root);            root=root->right;        }    }}
void post_order_traversal(TreeNode *root){    if(root==NULL)return;    stack<TreeNode> stack;    TreeNode *flag = NULL;    while(root!=NULL || !stack.empty()){        if(root){            stack.push(root);            root=root->left;        }else{            root=stack.top();            if(root->right && root->right != flag){                root=root->right;            }else{                root=stack.pop();                visit(root);                flag=root;                root=NULL;            }        }    }}

Hierarchical traversal:

void level_order_traversal(TreeNode *root){    if(root == NULL)return;    queue<TreeNode> queue;    queue.push(root);    while(!queue.empty()){        root = queue.pop();        visit(root);        if(root->left){            queue.push(root->left);        }        if(root->right){            queue.push(root->right);        }    }}

 

Summary of Binary Tree Algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.