Tree: Preorder Traversal

Sort by

recency

|

440 Discussions

|

  • + 0 comments
     public static void preOrder(Node root) {
            if(root != null){
                System.out.print(root.data+" ");
                preOrder(root.left);
                preOrder(root.right);
            }
        }
    
  • + 1 comment

    def preOrder(root): if(root==None): return 0 print(root.info, end=" ") preOrder(root.left) preOrder(root.right)

  • + 0 comments

    JAVA:

        public static void preOrder(Node node) {
            if (node != null) {
                System.out.print(node.data + " ");
                preOrder(node.left);
                preOrder(node.right);
            }
        }
    
  • + 0 comments
    def preOrder(root):
        result = []
        
        def traverse(node):
            if not node:
                return
            result.append(str(node.info))
            traverse(node.left)
            traverse(node.right)
            
        traverse(root)
        print(" ".join(result))
    
  • + 0 comments

    Python-

    def preOrder(root):
        #Write your code here
        cur = root
        if cur:
            print(cur.info, end=' ')
            if cur.left:
                preOrder(cur.left)
                
            if cur.right:
                preOrder(cur.right)
        return