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

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

这题简单,属于课本习题的难度,题目提示说用迭代试试,当然用递归也可以AC的。迭代的时候要注意栈的性质,不要push反了。

void preorderIter2(TreeNode *node, stack<TreeNode*>&st, vector<int>&ret) {
while (!st.empty()) {
TreeNode *top = st.top();
st.pop(); ret.push_back(top->val);
if (top->right) st.push(top->right);
if (top->left) st.push(top->left);
}
} vector<int> preorderTraversal(TreeNode *root) {
vector<int> ret;
if (!root) return ret;
stack<TreeNode *> st;
st.push(root);
preorderIter2(root, st, ret);
return ret;
}

迭代版

void preorderIter(TreeNode *node, vector<int> &ret) {
if (!node) return;
ret.push_back(node->val);
cout << node->val <<endl;
preorderIter(node->left, ret);
preorderIter(node->right, ret);
}

迭代

[LeetCode] Binary Tree Preorder Traversal的更多相关文章

  1. LeetCode: Binary Tree Preorder Traversal 解题报告

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

  2. [LeetCode] 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 @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先 ...

  4. LeetCode——Binary Tree Preorder Traversal

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

  5. LeetCode Binary Tree Preorder Traversal 先根遍历

    题意:给一棵树,求其先根遍历的结果. 思路: (1)深搜法: /** * Definition for a binary tree node. * struct TreeNode { * int va ...

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

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

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

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

  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. HDU 3999 二叉排序树

    The order of a Tree Problem Description The shape of a binary search tree is greatly related to the ...

  2. 51Nod 1766 树上的最远点对

    Description 一棵树,询问两个端点编号分别在在 \([a,b]\) 和 \([c,d]\) 两个区间中的最长链. Sol 线段树+ST表. 树上最长链可以合并,只需要合并两个区间最长链的两个 ...

  3. 全部springxml文件约束 applicationContext.xml

    <?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.sp ...

  4. ndk学习17: jni之Java调用C&C++

    一.Hello World 1. 定义函数原型 native关键字定义的函数即为jni函数 2.生成头文件 切换到src目录执行: (这个过程可以写脚本自动完成,比如自动拷贝到jni目录) javah ...

  5. JavaScript Coding 模式荟萃

    1.自运行的匿名函数 <script type="text/javascript" src="./js/jquery-1.7.2.js"></ ...

  6. 【smarty项目源码】模拟smarty模版文件的解析过程

    <?php class MyMiniSmarty{ //模版文件的存放路径 var $template_dir="./templates/"; //编译文件的存放路径 ,编译 ...

  7. 【云计算】marathon集群如何升级?

    Upgrading to a Newer Version We generally recommend creating a backup of the ZooKeeper state before ...

  8. 一步步教你Hadoop多节点集群安装配置

    1.集群部署介绍 1.1 Hadoop简介 Hadoop是Apache软件基金会旗下的一个开源分布式计算平台.以Hadoop分布式文件系统HDFS(Hadoop Distributed Filesys ...

  9. ODATA 云驱动 http://www.cdata.com/cloud/

    ODATA 云驱动   http://www.cdata.com/cloud/    目前支持:ORACLE.MS SQL . MYSQL. -------------- rssbus      ht ...

  10. 【leetcode】Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...