首页 > 其他 > 详细

[LeetCode] Binary Tree Preorder Traversal

时间:2015-02-24 01:52:38      阅读:284      评论:0      收藏:0      [点我收藏+]

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

For example:
Given binary tree {1,#,2,3},
技术分享
return [3,2,1].

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

递归实现代码

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/23 18:27
    *  @Status   : Accepted
    *  @Runtime  : 4 ms
******************************************************************/
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> result;
        helper(root, result);

        return result;
    }

    void helper(TreeNode *root, vector<int>& num)
    {
        if (root)
        {
            num.push_back(root->val);
            helper(root->left, num);
            helper(root->right, num);
        }
    }
};

非递归实现代码1

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/24 00:54
    *  @Status   : Accepted
    *  @Runtime  : 6 ms
******************************************************************/

class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> result;
        stack<TreeNode*> nodes;
        TreeNode *p = root;
        if (p)
        {
            nodes.push(p);
            while (!nodes.empty())
            {
                p = nodes.top();
                nodes.pop();
                result.push_back(p->val);
                if (p->right)
                {
                    nodes.push(p->right);
                }
                if (p->left)
                {
                    nodes.push(p->left);
                }
            }
        }

        return result;
    }
};

非递归实现代码2

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/24 01:06
    *  @Status   : Accepted
    *  @Runtime  : 4 ms
******************************************************************/

class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> result;
        stack<TreeNode*> nodes;
        TreeNode *p = root;
        while (p != NULL || !nodes.empty())
        {
            if (p != NULL)
            {
                while(p != NULL)
                {
                    result.push_back(p->val);
                    nodes.push(p);
                    p = p->left;
                }
            }
            else
            {
                p = nodes.top()->right;
                nodes.pop();
            }
        }

        return result;
    }
};

[LeetCode] Binary Tree Preorder Traversal

原文:http://blog.csdn.net/foreverling/article/details/43917129

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!