Tree: Preorder Traversal

  • + 0 comments

    My Java solution with linear time complexity and constant space complexity:

    public static void preOrder(Node root) {
            if(root == null) return; //no val to be printed
            
            //print val
            System.out.print(root.data + " ");
            
            //recursively traverse thru left nodes first, the through right nodes
            preOrder(root.left);
            preOrder(root.right);
        }