Binary Tree Preorder Traversal (leetcode 144)

原创 2015年07月10日 19:24:27

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?

Show Tags
Show Similar Problems


可以递归,可以用栈

/**
 * Definition for a binary tree node.
 * 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) {
        if (root == NULL) {
            return vector<int>();
        }
        vector<int> a;
        a.push_back(root->val);
        vector<int> l = preorderTraversal(root->left);
        vector<int> r = preorderTraversal(root->right);
        a.insert(a.end(), l.begin(), l.end());
        a.insert(a.end(), r.begin(), r.end());
        return a;
    }
};

// 非递归

/**
 * Definition for a binary tree node.
 * 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> a;
        if (root == NULL) {
            return a;
        }
        stack<TreeNode*> s;
        s.push(root);
        while(!s.empty()) {
            TreeNode* r = s.top();
            s.pop();
            a.push_back(r->val);
            if (r->right != NULL) {
                s.push(r->right);
            }
            if (r->left != NULL) {
                s.push(r->left);
            }
        }
        return a;
    }
};

相关文章推荐

leetcode(144):Binary Tree Preorder Traversal

题目Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes’ v...

Leetcode 144 - Binary Tree Preorder Traversal(二叉树前序遍历)

题意求二叉树的前序遍历。 思路分为递归实现和利用栈模拟递归实现。 递归实现的代码和思路很简单就不详细说了。关键是我们如何利用栈非递归的去实现。 关键思路就是:对于当前节点,我们递归的去对它的左子...
  • Lzedo
  • Lzedo
  • 2017年03月12日 14:43
  • 112

leetcode 144. Binary Tree Preorder Traversal-前序遍历|递归|非递归

原题链接:144. Binary Tree Preorder Traversal 【思路-Java】 用 stack 实现,也许有些读者对整个过程不是很熟悉,下面就以[1,2,3,4,5,null,6...

Binary Tree Preorder Traversal leetcode144

Given a binary tree, return the preorder traversal of its nodes’ values.For example: Given binary t...

Binary Tree Preorder Traversal - LeetCode 144

题目描述:Given a binary tree, return the preorder traversal of its nodes' values. For example: Given bi...
  • bu_min
  • bu_min
  • 2015年05月13日 20:35
  • 192

LeetCode题解-144-Binary Tree Preorder Traversal

尽管写LeetCode解答主要是为了自己整理思路,但是如果对您也正好有帮助,请点个赞给菜鸟一点鼓励吧 :-) 原题 解法概览 本题共有两种解法,1种递归,1种迭代。 前序遍历:...

leetcode_144_Binary Tree Preorder Traversal

具体思路和中序遍历是一致的,只是访问结点的值的时机不同罢了,具体思路参见:http://blog.csdn.net/mnmlist/article/details/44312315...

LeetCode笔记:144. Binary Tree Preorder Traversal

用循环操作来前序遍历二叉树

LeetCode 144 — Binary Tree Preorder Traversal(C++ Java Python)

题目:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Given a binary tree, return the p...

[leetcode] 144.Binary Tree Preorder Traversal

题目: Given a binary tree, return the preorder traversal of its nodes’ values.For example: Given bin...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Binary Tree Preorder Traversal (leetcode 144)
举报原因:
原因补充:

(最多只允许输入30个字)