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].

二叉树的先序遍历。递归算法太简单了,重点讨论一下非递归的。

我自己写的时候一直纠结于何时弹出结点。后来看了几个版本的,发现可以跳过这个部分。

贴上我最喜欢的版本,逻辑最清楚

//我最喜欢的版本 逻辑清晰
vector<int> preorderTraversal3(TreeNode *root)
{
if (root==NULL) return vector<int>();
vector<int> result;
stack<TreeNode *> treeStack;
treeStack.push(root);
while (!treeStack.empty())
{
TreeNode *temp = treeStack.top();
result.push_back(temp->val);
treeStack.pop();
if (temp->right!=NULL) //先压入右子树,后压入左子树,只有非空的时候压入,这样处理后的根据栈的后进先出,自然就是先处理左子树,再处理右子树了
treeStack.push(temp->right);
if (temp->left!=NULL)
treeStack.push(temp->left);
}
return result;
}

很短的版本:

vector<int> preorderTraversal2(TreeNode *root)
{
vector<int> ans;
vector<TreeNode *> v;
TreeNode * p = root;
while(p != NULL)
{
ans.push_back(p->val);
if(p->right != NULL)
{
v.push_back(p->right);
}
p = p->left;
if(p == NULL && !v.empty())
{
p = v.back();
v.pop_back();
}
}
return ans;
}

我自己写的,很长很繁琐。不好。

vector<int> preorderTraversal(TreeNode *root) {
vector<int> ans;
if(root == NULL) return ans;
vector<TreeNode *> v;
v.push_back(root);
while(!v.empty())
{
TreeNode * p = v.back();
if(p == NULL) //前面的压入右子树的过程中会出现NULL
{
v.pop_back(); //把这个指针弹出
if(!v.empty())
{
p = v.back();
v.pop_back(); //把再上一个指针也弹出
v.push_back(p->right); //压入刚才被弹出指针的右子树
}
}
else
{
ans.push_back(p->val); //先读取值
if(p->left != NULL)
{
v.push_back(p->left); //左子树不为空 压入左子树
}
else
{
v.pop_back(); //否则弹出该节点 并压入右子树
v.push_back(p->right);
}
}
}
return ans;
}

【leetcode】Binary Tree Preorder Traversal (middle)★的更多相关文章

  1. 【LeetCode】Binary Tree Preorder Traversal

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

  2. 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)

    这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...

  3. 【LeetCode】Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...

  4. 【Leetcode】【Medium】Binary Tree Preorder Traversal

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

  5. 【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)

    [Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For exam ...

  6. 【leetcode】Binary Tree Postorder Traversal

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...

  7. 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)

    这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

  8. 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)

    这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...

  9. 【leetcode】Binary Tree Postorder Traversal (hard) ☆

    二叉树的后序遍历 用标记右子树vector的方法 vector<int> postorderTraversal(TreeNode *root) { vector<int> an ...

随机推荐

  1. 浏览器内核与js引擎

    摘要: 面试一个大公司的时候问到了一个问题,让我谈谈主要的浏览器内核以及他们的特点,当时并没有详细的回答,回来之后自己在网上找了找资料,总结了下分享给大家. 简介: 在维基百科上是这样介绍浏览器内核的 ...

  2. Android手机同步电脑端google chrome书签

    我先声明:文中FQ 都是博客园自动将中文(fan qiang)转换为FQ的,并不是我本来写的就是FQ~~ 手机和电脑都必须要能登录google(Xee:几乎所有做开发的人都每天的生活都离不开谷歌了,可 ...

  3. 自动去除nil的NSDictionary和NSArray构造方法

    http://www.jianshu.com/p/a1e8d8d579c7 极分享 http://www.finalshares.com/

  4. hadoop 集群 加入一个新的存储节点和删除一个计算节点需要刷新集群状态命令

    加入一个新的存储节点和删除一个计算节点需要刷新集群状态命令 方式1:静态添加datanode,停止namenode方式 1.停止namenode 2.修改slaves文件,并更新到各个节点3.启动na ...

  5. css固定元素位置(fixed)

    来源:http://www.cnblogs.com/lecaf/archive/2011/03/25/fixed.html fixed是一种特殊的absolute,同样不占文档流,特殊的地方在于fix ...

  6. iOS开发——UI进阶篇(十三)UITabBarController简单使用,qq主流框架

    一.UITabBarController简单使用 // 程序加载完毕 - (BOOL)application:(UIApplication *)application didFinishLaunchi ...

  7. UOJ30——【CF Round #278】Tourists

    1.感谢taorunz老师 2.题目大意:就是给个带权无向图,然后有两种操作, 1是修改某个点的权值 2是询问,询问一个值,就是u到v之间经过点权的最小值(不可以经过重复的点) 操作数,点数,边数都不 ...

  8. [转载]localhost与127.0.0.1的区别

    原文链接:http://blog.csdn.net/xifeijian/article/details/12879395 很多人会接触到这个ip地址127.0.0.1.也许你会问127.0.0.1是什 ...

  9. iis不支持下载apk的解决办法

    添加mime类型即可 扩展名:".apk", MIME类型:"application/vnd.android.package-archive"

  10. nginx 下 bootstrap fa 字体异常问题

    server { listen 8082; # server_name 192.168.16.88; # root /home/ywt/workspace/kuF/web/statics; # aut ...