More

    BINARY TREE TRAVERSAL TECHNIQUES

    You can traverse a binary tree in three ways based on the order you visit the nodes – root node, the left subtree and the right subtree. Each subtree will again have root node, the left subtree and the right subtree.

    In-order

    • left subtree
    • root
    • right subtree

    Post-order 

    • left subtree
    • right subtree
    • root

    Pre-order 

    • root
    • left subtree
    • right subtree

    Notes:

    1. Order of subtree traversal in above is left followed by right. It may also be right followed by left. 
    2. Three of these can be considered as Depth First Traversal (DFS) techniques.

    RECURSIVE ALGORITHMS FOR TREE TRAVERSAL

    Inorder:

    public void inorder(TreeNode n)

    {

      if(n==null)

            return;

      inorder(n.left);

      print(n.data);

      inorder(n.right);

    }

    Preorder:

    public void preorder(TreeNode n)

    {

      if(n==null)

            return;

      preorder(n.left);

      preorder(n.right);

      print(n.data);

    }

    Postorder:

    public void postorder(TreeNode n)

    {

      if(n==null)

            return;

      print(n.data);

      postorder(n.left);

      postorder(n.right);

    }

    REFERENCES: 

    http://courses.cs.vt.edu/~cs3114/Fall09/wmcquain/Notes/T03a.BinaryTreeTheorems.pdf

    Recent Articles

    OAUTH – FREQUENTLY ASKED QUESTIONS FOR INTERVIEWS AND SELF EVALUATION

    Why is refresh token needed when you have access token? Access tokens are usually short-lived and refresh tokens are...

    SUMO LOGIC VIDEOS AND TUTORIALS

    Sumo Logic Basics - Part 1 of 2 (link is external) (Sep 29, 2016)Sumo Logic Basics - Part 2 of 2...

    GIT – USEFUL COMMANDS

    Discard all local changes, but save them for possible re-use later:  git stash Discarding local changes...

    DISTRIBUTED COMPUTING – RECORDED LECTURES (BITS)

    Module 1 - INTRODUCTION Recorded Lecture - 1.1 Introduction Part I – Definition

    BOOK REVIEW GUIDELINES FOR COOKBOOKS

    Whenever you add reviews for the book, please follow below rules. Write issues in an excel.Create an excel...

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Stay on op - Ge the daily news in your inbox