【Binary Tree Preorder Traversal】cpp

题目:

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?

代码:

/**
 * 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> ret_l, ret_r;
            if ( root ){
                ret_l = Solution::preorderTraversal(root->left);
                ret_l.insert(ret_l.begin(), root->val);
                ret_r = Solution::preorderTraversal(root->right);
                ret_l.insert(ret_l.end(), ret_r.begin(), ret_r.end());
            }
            return ret_l;
    }
};

tips:

trivial的递归版

===================================

再来一个non trivial的迭代版

/**
 * 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> ret;
            if ( !root ) return ret;
            stack<TreeNode*> sta;
            sta.push(root);
            while ( !sta.empty() )
            {
                TreeNode *tmp = sta.top();
                sta.pop();
                ret.push_back(tmp->val);
                if (tmp->right) sta.push(tmp->right);
                if (tmp->left) sta.push(tmp->left);
            }
            return ret;
    }
};

tips:

把递归调用改成人工堆栈:

1. 把根节点压进栈

2. 栈定元素出栈,把val加入到ret中

3. 注意:先压right入栈,再压left入栈(保证压入栈的元素都是非NULL)

循环1~3,直到栈空。

==============================================

还有更高端一些的Morris遍历算法:特点是空间复杂度是O(1)

http://www.cnblogs.com/AnnieKim/archive/2013/06/15/morristraversal.html

时间: 2025-01-04 03:01:38

【Binary Tree Preorder Traversal】cpp的相关文章

【Binary Tree Inorder Traversal】cpp

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 代码: /** * Definition for a binary tree node

【LeetCode】Binary Tree Preorder Traversal (2 solutions)

Binary Tree Preorder Traversal 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? 解法一:递归 /** * De

【LeetCode-面试算法经典-Java实现】【144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)】

[144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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

LeetCode: Binary Tree Preorder Traversal [144]

[题目] 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? [题意] 非递归返回先序遍历结果 [思路] 维护一个栈即可 [代码] /** *

[LeetCode][JavaScript]Binary Tree Preorder Traversal

Binary Tree Preorder Traversal 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? https://leetcod

LeetCode: Binary Tree Preorder Traversal 解题报告

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3},   1    \     2    /   3return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively?

40: Binary Tree Preorder Traversal

/************************************************************************/            /*       40:  Binary Tree Preorder Traversal                               */            /************************************************************************/ 

LeetCode:Binary Tree Preorder Traversal

题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode* left; 4 TreeNode* right; 5 TreeNode(int x): val(x), left(NULL),right(NULL) {} 6 }; 7 8 vector<int> preorderTraversal(TreeNode *root) //非递归的前序遍历(用栈实现)

[LeetCode 题解]: Binary Tree Preorder Traversal

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? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>