Skip to content

Binary Tree Traversals

node
struct node {
    int val;
    node *ls, *rs;
}

Preorder Traversal

void preorder(node *rt) {
    cout << rt -> val << endl;
    preorder(rt -> ls);
    preorder(rt -> rs);
}

Inorder Traversal

void inorder(node *rt) {
    inorder(rt -> ls);
    cout << rt -> val << endl;
    inorder(rt -> rs);
}

Postorder Traversal

void postorder(node *rt) {
    postorder(rt -> ls);
    postorder(rt -> rs);
    cout << rt -> val << endl;
}

Get Preorder Traversal


Comments