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. PL/SQL连接远程服务器数据库,出现ORA-12154: TNS: 无法解析指定的连接标识符。

    故障环境:上礼拜新装了一台服务器(win server2008r2),并在服务器上安装了ORACLE 11g database.且已经做好监听配置,开通了1521端口. 我又在局域网内另一台pc端装了 ...

  2. c# 获取 bios 序列号

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.M ...

  3. Combat 战斗任务

    发售年份 1977 平台 VCS 开发商 雅达利(Atari) 类型 射击 https://www.youtube.com/watch?v=2LxPEdUZOkE

  4. 常用oracle中系统表查询语句

    sqlplus / as sysdbaSQL>select status from v$instance;1.查看最大连接数show parameter processes;2.查询oracle ...

  5. Day 15 模块

    模块 ```python'''模块:一系列功能的集合体 定义模块:创建一个py文件就是一个模块,该py文件名就是模块名 使用模块:在要使用模块的文件中,通过 import 模块名 来导入模块''' ' ...

  6. celery+Rabbit MQ实战记录

    基于以前的一篇文章,celery+Rabbit MQ的安装和使用, 本文更加详细的介绍如何安装和使用celey, Rabbit MQ. 并记录在使用celery时遇到的一些问题. 1.安装 Rabbi ...

  7. 一个简单的python爬虫程序

    python|网络爬虫 概述 这是一个简单的python爬虫程序,仅用作技术学习与交流,主要是通过一个简单的实际案例来对网络爬虫有个基础的认识. 什么是网络爬虫 简单的讲,网络爬虫就是模拟人访问web ...

  8. C语言volatile关键字的用法

    volatile关键字的意义在于让被关键字修饰的变量每次使用时都重新去主内存里读取变量,而不是从高速缓存去读取. int a = 1; 普通变量定义之后,若要使用它,都会 先把它的值从主内存拷贝到高速 ...

  9. 【linux】之日志查看

    搜索日志 -n 显示行号 grep 1570xxxx -n callback.tomcat-catalina-out 显示从第多少行~多少行 sed -n '464913,465020p' callb ...

  10. Azkaban 使用问题及解决(一)

    什么是Azkaban Azkaban是一款基于Java编写的任务调度系统 任务调度:有四个任务脚A.B.C.D,其中任务A与任务B可以并行运行,然后任务C依赖任务A和任务B的运行结果,任务D依赖任务C ...