码迷,mamicode.com
首页 > 其他好文 > 详细

【Leetcode】Binary Tree Preorder Traversal

时间:2014-08-20 14:01:43      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   strong   for   ar   div   amp   

Given a binary tree, return the preorder traversal of its nodes‘ values.

For example:
Given binary tree {1,#,2,3},

   1
         2
    /
   3

 

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

 

1st  ( 8 tries)

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution 
{
public:
    vector<int> preorderTraversal(TreeNode *root) 
    {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        vector<int> re;
        /*
        preorder(root,re);
        return re;
        */
        /*iteratively*/
        stack<TreeNode *> st;
        if(root == NULL)
            return re;
        re.push_back(root->val);
        st.push(root);
        TreeNode *iter = root->left;
        /*this is important,while condition*/
        while(!st.empty() || /*this is import*/iter != NULL)
        {
            while(iter != NULL)
            {
                re.push_back(iter->val);
                st.push(iter);
                iter = iter->left;
            }
            iter = st.top();
            st.pop();
            iter = iter->right;
        }
        return re;
    }
    /*
    void preorder(TreeNode *root,vector<int> &re)
    {
        if(root == NULL)
            return;
        re.push_back(root->val);
        preorder(root->left,re);
        preorder(root->right,re);
    }
    */
};

  

2nd ( 6 tries)

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> ans;
    vector<int> preorderTraversal(TreeNode *root) {
        //recursive
        /*
        recursive(root);
        return ans;
        */
        //iteratively
        stack<TreeNode *> s;
        if(root == NULL)
            return ans;
        TreeNode *cur = root;
        //first check iterative pointor is not NULL or stack is not NULL,
        //it imply that there is some node you can scan
        while( cur != NULL || !s.empty() ) {
            //iterative all the left node
            while(cur != NULL) {
                ans.push_back(cur->val);
                s.push(cur);
                cur = cur->left;
            }
            //if all left node scaned,goto the right child tree
            cur = s.top();
            s.pop();
            cur = cur->right;
        }
        return ans;
    }
    /*
    void recursive(TreeNode *root) {
        //step 1
        if(root == NULL)
            return;
        //step 2
        
        //step 3
        ans.push_back(root->val);
        recursive(root->left);
        recursive(root->right);
    }
    */
};

  

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> ans;
        TreeNode *cur = NULL,*pre = NULL;
        cur = root;
        while( cur ) {
            if( cur->left == NULL ) {
                //in-order here!!!
                //pre-order here!!!
                ans.push_back(cur->val);
                cur = cur->right;
            }
            else {
                pre = cur->left;
                while( pre->right != NULL && pre->right != cur ) {
                    pre = pre->right;
                }
                if( pre->right == NULL ) {
                    pre->right = cur;
                    //pre-order here!!!
                    ans.push_back(cur->val);
                    cur = cur->left;
                }
                else {
                    pre->right = NULL;
                    //in-order here!!!
                    cur = cur->right;
                }
            }
        }
        return ans;
    }
};

  

【Leetcode】Binary Tree Preorder Traversal,布布扣,bubuko.com

【Leetcode】Binary Tree Preorder Traversal

标签:des   blog   io   strong   for   ar   div   amp   

原文地址:http://www.cnblogs.com/weixliu/p/3924397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!