Given a binary tree, return the preorder traversal of its nodes' values.

Example:

Input: [,null,,]

    \

    /

Output: [,,]

Follow up: Recursive solution is trivial, could you do it iteratively?

方法一:使用迭代(C++)

 vector<int> preorderTraversal(TreeNode* root) {
vector<int> res={};
if(!root)
return res;
stack<TreeNode*> s;
TreeNode* cur=root;
while(!s.empty()||cur){
while(cur){
res.push_back(cur->val);
s.push(cur);
cur=cur->left;
}
cur=s.top();
s.pop();
cur=cur->right;
}
return res;
}

方法二:使用递归,更简单,但是效率较低

 void preOrder(TreeNode* root,vector<int>& m){
if(!root)
return;
m.push_back(root->val);
preOrder(root->left,m);
preOrder(root->right,m);
} vector<int> preorderTraversal(TreeNode* root) {
vector<int> res={};
if(!root)
return res;
preOrder(root,res);
return res;
}

LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++的更多相关文章

  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】Binary Tree Preorder Traversal(二叉树的前序遍历)

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

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

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

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

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

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

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

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

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

  7. Leetcode 144 Binary Tree Preorder Traversal 二叉树

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

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

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

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

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

随机推荐

  1. Where are your from!!!!!!!!!!!! !Baby! {封装}

    在说Java 三个特性之前,我们先了解一下什么是面向对象,以及为什么Java是面向对象的语言. 面向对象是区别于面向过程的一种编程的思想.我们可以通过这个例子冰箱装大象的例子来了解一下面向对象与面向过 ...

  2. shiro注解

    shiro注解权限控制-5个权限注解 RequiresAuthentication: 使用该注解标注的类,实例,方法在访问或调用时,当前Subject必须在当前session中已经过认证. Requi ...

  3. Linux第八节课学习笔记

    su命令可以切换用户身份,一般不用,而是用sudo. visudo命令中可执行命令列表不用ALL,我们可以先使用whereis命令找出命令所对应的保存路径,然后把配置文件第99行的用户权限参数修改成对 ...

  4. python实现linux下文件遍历

    import os def getAllFile(*names): if len(names) == 0: return "" else: allList = [] for nam ...

  5. PymongoDB_study

    import pymongo client = pymongo.MongoClient(host='localhost',port=27017)#连接数据库 #db = client.test#指定数 ...

  6. Magento 1.9.x 子分类无法访问

    app/code/core/Mage/Catalog/Model/Url.php 大概 ~807: Change: if ($product->getUrlKey() == '' &&a ...

  7. 使用python调用其他脚本

    cmd = '<command line string>' print(cmd) p = subprocess.Popen(args=cmd, shell=True, stdout=sub ...

  8. python MySQL执行SQL查询结果返回字典

    写自动化测试的时候我希望执行数据库前置任务,把数据库查询的结果作为请求的参数,但是正常返回结果为列表嵌套里面,这样就会影响到关键字准确的获取,特别的受限于SQL的查询字段的的顺序,所以希望返回的单条数 ...

  9. win2008安装并配置zabbix3.4 agent

    agent 下载地址: https://www.zabbix.com/download_agents 下载解压后有2个文件夹,一个是bin,存放32位和64位执行程序,一个是conf目录存放着配置文件 ...

  10. 【java】抽象类和接口区别

    1.语法层面上的区别 1)抽象类可以提供成员方法的实现细节,而接口中只能存在public abstract 方法: 2)抽象类中的成员变量可以是各种类型的,而接口中的成员变量只能是public sta ...