方法一:(迭代)

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. VC比例放大缩小

    CRect rect; ::GetWindowRect(m_hWnd, rect); ScreenToClient(rect); m_nDlgWidth = rect.right - rect.lef ...

  2. javascript实例备忘

    一,javascript动态显示: 如显示效果上图所示: 如图显示鼠标放在百度谷歌等字样上市动态显示其内容明细:代码如下:<head><title></title> ...

  3. iPhone 6 Screen Size and Web Design Tips

    Apple updated its iPhone a bit ago making the form factor much bigger. The iPhone 6 screen size is b ...

  4. Java—Servlet技术

    1  Servlet 概述 Servlet简介——开发动态web资源的技术Sun公司在API提供了一个servlet接口,如开发一个java程序向浏览器输出数据:1)编写一个java类,实现servl ...

  5. guava学习--monitor

    转载:https://my.oschina.net/realfighter/blog/349924   https://my.oschina.net/realfighter/blog/349926 M ...

  6. php开发必备小工具

    /*递归删除目录及目录下的文件*/ function del_dir($dir){ $files = new DirectoryIterator($dir); foreach ($files as $ ...

  7. Win10

    安装 调优 关闭cortana 对于SSD: 关闭windows search , superfetch服务,减少磁盘读写 关闭动画(个性化里面) 开启项优化 休眠文件(powercfg -h off ...

  8. kendo模板 Uncaught Error: Invalid template:' 报错

    I was having a problem with a grid toolbar template because of a # in a hrefWorked out that I needed ...

  9. css3选择器详解

    css中除了早先最早的,ID选择器,class选择器一些以外在css3中新加入了新的选择器,新选择器的使用大大的方便了我们的编程,下面我就说一些css3的选择器的使用方法, p       选择了所有 ...

  10. APIJSON,让接口见鬼去吧!

    我: APIJSON,让接口见鬼去吧! https://github.com/TommyLemon/APIJSON 服务端: 什么鬼? 客户端: APIJSON是啥? 我: APIJSON是一种JSO ...