You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varpreorderTraversal=function(root){if(!root)return[];//We need to put a root in the stack first, because Pre-order traverse from root => left => rightletstack=[root];letarr=[];while(stack.length){letcurr=stack.pop();arr.push(curr.val);// we want to push right subtree is because the left subtree will be visit first then.if(curr.right){stack.push(curr.right);}if(curr.left){stack.push(curr.left);}}returnarr;};
The text was updated successfully, but these errors were encountered:
Given the root of a binary tree, return the preorder traversal of its nodes' values.
A Very Straightforward Solution
An O(N) Solution may be interesting
The text was updated successfully, but these errors were encountered: