Showing posts with label preorder traversal. Show all posts
Showing posts with label preorder traversal. Show all posts

Thursday, January 26, 2017

Binary Tree Pre Order Traversing Code

PreOrder Traversing Code in BST:

=>

    public void printPreOrder(){
       
        if(root == null){
            return;
        }
       
        Stack stack = new Stack(numNodes);
       
        stack.push(root);
       
        while(! stack.isEmpty()){
           
            Node node = stack.pop();
            System.out.println(node.data);
           
            if(node.right ! = null){
                stack.push(node.right);
            }
            if(node.left != null){
                stack.push(node.left);
            }
        }
    }