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. ASC47B borderless

    题目描述 border集合为{NULL,str}的串str称为borderless串. border即KMP里的那个. 字符集{'a','b'},给定长度n,求第k(给定)小的borderless串. ...

  2. 【架构】linkerd:来自Twitter为微服务而生的开源RPC解决方案

    大家要如何以规模化方式运维微服务应用程序?实践当中会出现哪些问题,我们又该如何加以解决?在大规模与非预测性工作负载场景当中,我们需要满足哪些条件才能运行一款大型微服务应用程序,而又能够确保不必受到功能 ...

  3. POJ 1661

    http://poj.org/problem?id=1661 这是一道DP的题目,求最优解 上面的这一个题是对于那个重左边开始上的函数的解释 题目要求的是从最高掉下来的小时间,那么我们就可以求从最低处 ...

  4. poj 1562

    这道题主要注意输入的问题,以及对周围搜索时的注意,要使用递归,多次调用,附上一组数据 11 20*@*@*@@@**@*@**@@@*****@*@*@*@*@****@**@*@*@*@*@*@*@ ...

  5. Bitmap vs 2Bitmap的实现

    [本文链接] http://www.cnblogs.com/hellogiser/p/bitmap-vs-2bitmap.html [题目] 在2.5亿个整数找出不重复的整数,内存不足以容纳着2.5亿 ...

  6. spring + myBatis 常见错误:注解事务不回滚

    最近项目在用springMVC+spring+myBatis框架,在配置事务的时候发现一个事务不能回滚的问题. 刚开始配置如下:springMVC.xml配置内容: spring.xml配置内容 从上 ...

  7. linux下用cronolog分割apache日志

    linux下用cronolog分割apache日志,大神莫拍砖,菜鸟留一记录,小白请默默转载.连linux登陆和vi编辑都不会的,请默默关闭此页面.入正题 说明:淡绿色底的为linux命令,其他的为备 ...

  8. ios NSNotificationCenter 收到通知后的执行线程

    https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Articles/Thread ...

  9. WIZnet官方网盘

    之前使用过 WIZnet 的TCP/IP 解决方案,资源较少, 偶然发现此网盘,不敢独享,访问 请戳此处.

  10. 【Git】笔记4 分支管理1

    1.创建与合并分支 一开始的时候,master分支是一条线,Git用master指向最新的提交,再用HEAD指向master,就能确定当前分支,以及当前分支的提交点: 每次提交,master分支都会向 ...