144. Binary Tree Preorder Traversal

提交网址: https://leetcode.com/problems/binary-tree-preorder-traversal/

Total Accepted: 118355 Total Submissions: 297596 Difficulty: Medium

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?

分析:

借助栈实现非递归先序遍历算法的方法如下:
1)将二叉树的根结点作为当前结点。
2)若当前结点非空,则先访问该结点,并将该结点进栈,再将其左孩子结点作为当前结点,重复步骤2),直到当前结点为NULL为止。
3)若栈非空,则栈顶结点出栈,并将当前结点的右孩子结点作为当前结点。
4)重复步骤2)、3),直到栈为空且当前结点为NULL为止。

AC代码:

#include<iostream>
#include<vector>
#include<stack>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left, *right;
TreeNode(int x): val(x), left(NULL), right(NULL) {}
}; class Solution
{
public:
vector<int> preorderTraversal(TreeNode *root)
{
vector<int> res;
TreeNode *p;
stack<TreeNode*> s;
p=root; while(!s.empty() || p!=NULL)
{
if(p!=NULL)
{
res.push_back(p->val);
s.push(p);
p=p->left;
}
if(p==NULL)
{
p=s.top();
s.pop();
p=p->right;
}
}
return res;
}
}; // 以下为测试部分
/*
int main()
{
Solution sol;
vector<int> res; TreeNode *root = new TreeNode(1);
root->right = new TreeNode(2);
root->right->left = new TreeNode(3); res=sol.preorderTraversal(root); for(int i:res)
cout<<i<<" "; // 此处为vector遍历的方法,C++11标准支持
return 0;
}
*/

C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)的更多相关文章

  1. LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...

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

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

  3. Leetcode 144 Binary Tree Preorder Traversal 二叉树

    二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...

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

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

  5. 144 Binary Tree Preorder Traversal 二叉树的前序遍历

    给定一棵二叉树,返回其节点值的前序遍历.例如:给定二叉树[1,null,2,3],   1    \     2    /   3返回 [1,2,3].注意: 递归方法很简单,你可以使用迭代方法来解决 ...

  6. 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

    题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...

  7. [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历

    关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...

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

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

  9. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

随机推荐

  1. squid故障汇总

    1.COSS will not function without large file support (off_t is 4 bytes long. Please reconsider recomp ...

  2. C语言内存四区的学习总结(一)---- 静态区

    最近重新学习C语言相关知识,重新提到内存四区的概念,那么在之前的学习的基础上,在这儿做一个简单的总结与分享. 一.内存四区建立的流程 可以简单直观的查看下面的这个图片,直接的说明我们的程序在内存中是如 ...

  3. idea安装了Mybaits Plugin插件后,启动不起来了

    之前安装了一些插件,谁知道重启完了之后,直接启动不起来了,报错信息如下: cannot load project fatal error initializing plugin com.seven7. ...

  4. Unity使用代码动态给按钮赋值各个状态下的图片

    一个小知识点,怕忘记,所以记录下.废话不多说,直接上代码: 未赋值之前: 使用下面代码赋值: using UnityEngine; using UnityEngine.UI; public class ...

  5. Servlet创建完美教程

    简介:Servlet其实是Server Let的缩写,是服务器端应用程序.  java中有一个applet是客户端应用程序,与servlet对应.applet已经过时.Servlet作用:能在B/S架 ...

  6. appium安装问题集锦

    问题一: MacBook-Air:Cellar$ npm -v dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.61.dyl ...

  7. windows mysql zip 安装

    https://www.cnblogs.com/iathanasy/p/8461429.html

  8. 【python-appium】Appium的一些坑问题错误解决 与 技巧集锦

    问题 1. error: Failed to start an Appium session, err was: Error: Requested a new session but one was ...

  9. window系统中 mongodb创建用户名和密码

    use admindb.createUser({user:"root",pwd:"root",roles:[{"role":"us ...

  10. FFmpeg开发实战(一):FFmpeg 打印日志

    在Visual Studio 开发(二):VS 2017配置FFmpeg开发环境一文中,我们配置好了FFmpeg的开发环境,下面我们开始边实战,边学习FFmpeg. 首先,我们要学习的就是FFmpeg ...