Easy solution to understand


  • 0
    N
    class Solution {
    public:
        vector<int> preorderTraversal(TreeNode* root) {
            vector<int> result;
    		stack<TreeNode*> traStack;
    		
    		TreeNode *cur = root;
    		while(cur!=NULL || !traStack.empty())
    		{
    		    while(cur!=NULL)
    		    {
    		    result.push_back(cur->val);
    		    traStack.push(cur);
    		    cur = cur->left;
    		    }
    		    TreeNode* top = traStack.top();
    		    traStack.pop();
    		    cur = top->right;
    		}
    
    		return result;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.