题目:

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?

解题思路:

利用栈实现,很简单的一题,不多说了,直接上代码

实现代码:

#include <iostream>
#include <vector>
#include <stack>
using namespace std; /**
Given a binary tree, return the preorder traversal of its nodes' values.
*/ struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; void addNode(TreeNode* &root, int val)
{
if(root == NULL)
{
TreeNode *node = new TreeNode(val);
root = node;
}
else if(root->val < val)
{
addNode(root->right, val);
}
else if(root->val > val)
{
addNode(root->left, val);
}
} void printTree(TreeNode *root)
{
if(root)
{
cout<<root->val<<" ";
printTree(root->left);
printTree(root->right);
}
} class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> ivec;
if(root)
{
stack<TreeNode *> tstack;
TreeNode *p = root;
while(p || !tstack.empty())
{
while(p)
{
ivec.push_back(p->val);
tstack.push(p);
p = p->left;
}
if(!tstack.empty())
{
TreeNode *t = tstack.top();
tstack.pop();
p = t->right;
}
} }
return ivec; }
};
int main(void)
{
TreeNode *root = new TreeNode();
addNode(root, );
addNode(root, );
addNode(root, );
addNode(root, );
printTree(root);
cout<<endl; Solution solution;
vector<int> v = solution.preorderTraversal(root);
vector<int>::iterator iter;
for(iter = v.begin(); iter != v.end(); ++iter)
cout<<*iter<<" ";
cout<<endl; return ;
}

LeetCode144:Binary Tree Preorder Traversal的更多相关文章

  1. LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)

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

  2. lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历

    题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...

  3. Binary Tree Preorder Traversal on LeetCode in Java

    二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...

  4. LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium

    题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...

  5. LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal

    Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its ...

  6. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

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

  7. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  8. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  9. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

随机推荐

  1. SharePoint 2010 外部数据类型

    需求描述: 在sharepoint 2010有时需要访问外部的数据(如sql数据库). 参考文章: 外部内容类型 Business Connectivity Services ECT外部内容类型 Se ...

  2. webpack 构建同时适用于手机和电脑的调试服务器

    plugins plugins: [ new HtmlWebpackPlugin({ // 使用模板同时生成 pc.html和mobile.html title: 'pc', filename: 'p ...

  3. android显示和隐藏软键盘

    显示键盘: EditText editText.setFocusable(true); editText.setFocusableInTouchMode(true); editText.request ...

  4. IBM MQ 学习

    import java.io.IOException; import java.util.HashMap; import java.util.Map; import com.ibm.mq.MQC; i ...

  5. 【z】Storm - the world's best IDE framework for .NET

    http://www.codeproject.com/Articles/42799/Storm-the-world-s-best-IDE-framework-for-NET Storm - the w ...

  6. Oracle LOOP循环控制语句

    在PL/SQL中可以使用LOOP语句对数据进行循环处理,利用该语句可以循环执行指定的语句序列.常用的LOOP循环语句包含3种形式:基本的LOOP.WHILE...LOOP和FOR...LOOP. LO ...

  7. Bad Hair Day

    /* Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-c ...

  8. noi 1997 最优乘车

    H城是一个旅游胜地,每年都有成千上万的人前来观光.为方便游客,巴士公司在各个旅游景点及宾馆,饭店等地都设置了巴士站并开通了一些单程巴上线路.每条单程巴士线路从某个巴士站出发,依次途经若干个巴士站,最终 ...

  9. jquery节点获取

    jQuery.parent(expr)  找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(& ...

  10. Laravel - Opening Multiple Projects

    On this page: Basics Opening multiple projects Deleting a project from view Important notes Basics P ...