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

思路:前序遍历,“根,左子树,右子树”

 #include <iostream>
#include <vector>
#include <stack>
using namespace std; 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> res;
stack<TreeNode*> s;
if (!root) {
return res;
} s.push(root);
while(!s.empty()) {
TreeNode *p = s.top(); s.pop();
res.push_back(p->val);
if (p->right) {
s.push(p->right);
}
if (p->left) {
s.push(p->left);
}
}
return res;
}
}; int main() {
return ;
}

leetcode - [7]Binary Tree Preorder Traversal的更多相关文章

  1. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  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】Binary Tree Preorder Traversal

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

  4. [LeetCode 题解]: Binary Tree Preorder Traversal

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a bi ...

  5. 【leetcode】Binary Tree Preorder Traversal (middle)★

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

  6. Java for LeetCode 144 Binary Tree Preorder Traversal

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

  7. leetcode 144. Binary Tree Preorder Traversal ----- java

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

  8. leetcode 题解:Binary Tree Preorder Traversal (二叉树的先序遍历)

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

  9. Java [Leetcode 144]Binary Tree Preorder Traversal

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

  10. (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

随机推荐

  1. docker-ce-17.09 网络基础配置

    一.端口映射实现访问容器 1.我们先从pull一个nginx镜像,然后后台运行该镜像 > docker pull nginx > docker run -d -P nginx:latest ...

  2. Struts框架的数据封装二之模型驱动方式

    Struts2中提供了两类数据封装的方式? * 第二种方式:模型驱动 > 使用模型驱动的方式,也可以把表单中的数据直接封装到一个JavaBean的对象中,并且表单的写法和之前的写法没有区别! & ...

  3. BCHABC/BCHSV的矛盾所在

    BCHABC: 将BCH以后发展智能合约: 消息方面: 吴忌寒:BCH分叉不可避免 未来可能继续分叉 近日,吴忌寒在北大光华管理学院的区块链培训课程上发表演讲表示,在这种言论自由地环境下,我认为分裂就 ...

  4. Logback报错 no applicable action for [Encoding], current ElementPath is [[configuration][appender][Encoding]]

    老版本是0.9,移到springboot项目,解决办法,删除xml配置文件节点<Encoding>UTF-8</Encoding>

  5. Git 安装和使用教程(更加详细)

    转载至:https://www.cnblogs.com/smuxiaolei/p/7484678.html#undefined Git 安装和使用教程 git 提交 全部文件 git add .  g ...

  6. vue之微信登录

    参考文章https://www.cnblogs.com/examine/p/4634947.html 微信开放平台和公众平台的区别1.公众平台面向的是普通的用户,比如自媒体和媒体,企业官方微信公众账号 ...

  7. stl中顺序性容器,关联容器两者粗略解释

    什么是容器 首先,我们必须理解一下什么是容器,在C++ 中容器被定义为:在数据存储上,有一种对象类型,它可以持有其它对象或指向其它对像的指针,这种对象类型就叫做容器.很简单,容器就是保存其它对象的对象 ...

  8. [SoapUI] 判断工程下某个文件是否存在,存在就删除

    def excelName = "AllTests-Fails" String projectPath = context.expand( '${projectDir}' ) St ...

  9. db2建立类似oracle的dblink

    db2 catalog tcpip node rmt_node remote 127.0.0.1 server 50000; --db2 catalog database rmt_db as rmt_ ...

  10. PHP图片处理库Grafika详细教程

    转载自51CTO 开发频道 1.图像基本处理:http://developer.51cto.com/art/201611/520928.htm 2.图像特效处理模块:http://developer. ...