Tree: Preorder Traversal

Sort by

recency

|

81 Discussions

|

  • + 0 comments

    Why is the php so messed up? why doesn't it make sense compared to python and js ?

  • + 0 comments
    void preOrder(Node *root) {
    
                if (root == nullptr){
                return;
        }
    
        std::cout << root->data << " ";
        preOrder(root->left);
        preOrder(root->right);}
    
    }
    
  • + 0 comments

    Why is the C++ 14 solution template formulated in such a weird way that you cannot include additional libraries (such as stack for this question)? The C++ 11 and 20 templates are totally fine. I wish the solution templates are all standardized like Leetcode does.

  • + 0 comments
    if(root == nullptr)
              return;
               
    int data = root->data;
    cout<<data <<  " ";
    preOrder(root->left);
    preOrder(root->right);
    
  • + 0 comments

    Python solution:

    def preOrder(root):
        def recursion(root, results):
            if root is None:    # base case: empty subtree
                return
            results.append(root.info)
            recursion(root.left, results)
            recursion(root.right, results)        
        results = []
        recursion(root, results)
        print(" ".join(map(str, results)))