方法一:(迭代)

class Solution
{
public:
vector<int> preorderTraversal(TreeNode* root)
{
vector<int> v; if(root == NULL) return v; stack<TreeNode *> s;
s.push(root); TreeNode *curNode = NULL;
while(!s.empty())
{
curNode = s.top(); s.pop();
v.push_back(curNode->val); // 注意这里的顺序
if(curNode->right) s.push(curNode->right);
if(curNode->left) s.push(curNode->left);
} return v;
}
};

方法二:(递归)

Binary Tree Preorder Traversal -- LEETCODE 144的更多相关文章

  1. Binary Tree Preorder Traversal —— LeetCode

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

  2. Binary Tree Preorder Traversal leetcode java

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

  3. Binary Tree Preorder Traversal -- leetcode

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

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

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

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

    144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...

  6. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

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

  7. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  8. 【LeetCode】Binary Tree Preorder Traversal

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

  9. Binary Tree Preorder Traversal on LeetCode in Java

    二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...

随机推荐

  1. 游戏外挂四之利用CE和OD查找被选中怪物和怪物列表

    合肥程序员群:49313181.    合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入)Q  Q:408365330     E-Mail:egojit@qq.com 这一节我们利 ...

  2. Eclipse 安装中文插件

    从官网下载下来的Eclipse都是英文版,下载后解压,运行Eclipse. 地址:http://www.eclipse.org/babel/downloads.php .启动Eclipse,在菜单,“ ...

  3. SQLite3中自增主键归零方法

    当SQLite数据库中包含自增列时,会自动建立一个名为 sqlite_sequence 的表.这个表包含两个列:name和seq.name记录自增列所在的表,seq记录当前序号(下一条记录的编号就是当 ...

  4. H:Highways

    总时间限制: 1000ms 内存限制: 65536kB描述The island nation of Flatopia is perfectly flat. Unfortunately, Flatopi ...

  5. 聚光灯下的熊猫TV技术架构演进

    2015年开始的百播大战,熊猫TV是其中比较特别的一员. 说熊猫TV是含着金钥匙出生的公子哥不为过.还未上线,就频频曝光,科技号,微博稿,站上风口浪尖.内测期间更是有不少淘宝店高价倒卖邀请码,光内测时 ...

  6. 无废话SharePoint入门教程三[创建网站集和网站]

    一.前言 前两篇文章讲解了什么是SharePoint,并且介绍了在SharePoint中一些常用的概念.但概念终究是概念,我们还是要脚踏实地的去动手实践.下面的文章对于了解SharePoint的人来说 ...

  7. ADO总结测试数据库

    create database ADO测试 go use ADO测试 go create table Student ( Code ) not null primary key,--学生编号,主键 N ...

  8. javascript 手势缩放 旋转 拖动支持:hammer.js

    原文: https://cdn.rawgit.com/hammerjs/hammer.js/master/tests/manual/visual.html /*! Hammer.JS - v2.0.4 ...

  9. dede日期时间标签调用大全

    dedecms最强大的功能就是调用标签,可以变换出各种样式的文章形式出来,本节将DEDECMS调用时间的样式做一个总结,基本所有的时间调用样式都有了,日期时间格式 (利用strftime()函数格式化 ...

  10. js判断鼠标向上滚动并浮动导航

    <div id="Jnav"> <ul class="nav"> <li><a href="#"& ...