We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Tree: Preorder Traversal
Tree: Preorder Traversal
Sort by
recency
|
452 Discussions
|
Please Login in order to post a comment
In some languages like Kotlin it gives only a main fucntion which doesn't have a Node class. And there's no explanation how the single array, that the input is giving, is sorted... I spent hours trying to figure out what the tree from the input should look like and I was trying to write a function to create the tree with Node class... So then I switched to Java and saw that the node object is given...
void solve(Node* root) { if(root == NULL) { return; } cout<data<<" "; solve(root->left); solve(root->right); }
solve(root);
Why it was showing main method in this?
My Java 8 solutions
Recursive approach
Iterative approach
My Java solution with linear time complexity and constant space complexity: