前言

【LeetCode 题解】系列传送门:  http://www.cnblogs.com/double-win/category/573499.html

1.题目描述

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?

2. 题意

先序遍历二叉树,递归的思路是普通的,能否用迭代呢?

3. 思路

非递归思路:<借助stack>

    vector<int> preorderTraversal(TreeNode *root) {stack<TreeNode* > st;vector<int> vi;vi.clear();if(!root) return vi;st.push(root);while(!st.empty()){TreeNode *tmp = st.top();vi.push_back(tmp->val);    st.pop();if(tmp->right) st.push(tmp->right);if(tmp->left) st.push(tmp->left);}return vi;}

递归思路:

class Solution {
private:vector<int> vi;
public:vector<int> preorderTraversal(TreeNode *root) {vi.clear();if(!root) return vi;preorder(root);return vi;}void preorder(TreeNode* root){if(!root) return;vi.push_back(root->val);preorder(root->left);preorder(root->right);}
};

4.相关题目

(1)二叉树的中序遍历:

(2)二叉树的后序遍历:

(3) 二叉树系列文章:

作者:Double_Win

出处: http://www.cnblogs.com/double-win/p/3896010.html 

声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~

转载于:https://www.cnblogs.com/double-win/p/3895822.html

[LeetCode 题解]: Binary Tree Preorder Traversal相关推荐

  1. 【二叉树迭代版前序遍历】LeetCode 144. Binary Tree Preorder Traversal

    LeetCode 144. Binary Tree Preorder Traversal Solution1:递归版 二叉树的前序遍历递归版是很简单的,前序遍历的迭代版相对是最容易理解的. 迭代版链接 ...

  2. [Lintcode]66. Binary Tree Preorder Traversal/[Leetcode]144. Binary Tree Preorder Traversal

    66. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal 本题难度: Easy/Medium Topic: Bina ...

  3. leetcode 144. Binary Tree Preorder Traversal

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

  4. Leetcode - 144. Binary Tree Preorder Traversal (层次遍历)

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

  5. leetcode 144. Binary Tree Preorder Traversal ----- java

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

  6. [LeetCode] 144. Binary Tree Preorder Traversal Java

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

  7. LeetCode 145. Binary Tree Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-postorder-traversal/ 题目: Given a binary tree, retu ...

  8. LeetCode 144. Binary Tree Preorder Traversal--二叉树前序遍历--反向压栈--迭代-栈,递归--C++,Python解法

    题目地址:Binary Tree Preorder Traversal - LeetCode Given a binary tree, return the preorder traversal of ...

  9. LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium

    题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode* ...

最新文章

  1. HTML5音乐播放器(四):播放列表与播放方式
  2. 创建一个dynamics CRM workflow (三) - Creating Configuration Entity for Custom Workflow
  3. 【体验】ESP32-CAM可能是最便宜的“监控”方案,ESP32-CAM程序下载调试
  4. 近似最近邻搜索ANN(Approximate Nearest Neighbor)
  5. python中shutil模块的用法_python中的os,shutil模块的定义以及用法
  6. python中的format方法和int方法
  7. 首次开源!一行代码轻松搞定中英文语音识别、合成、翻译核心功能!
  8. (四)DOM对象和jQuery对象
  9. 宜创科技:低代码技术赋能企业数字化
  10. 知行之桥®中文版EDI系统正式发布
  11. GPS-GGA数据格式
  12. 怎么复制豆丁网的文字
  13. 火车票电子客票系统已全面上线,如何识别多种身份有效证件?
  14. 智能名片小程序名片详情页功能实现关键代码
  15. 有别于普通专线的BGP线路
  16. GROMACS .mdp 选项翻译及笔记
  17. 愿得一人心——祭奠······埋葬我218的爱情
  18. Python基础入门自学——18--操作Excel-工作实践需求
  19. 数据库作业17:第六章总结
  20. 创业计划书和商品计划书PPT模板-朴尔PPT

热门文章

  1. 中心频率和一些概念解释
  2. ajax is failed怎么办,我在AJAX中遇到了问题
  3. 计算机硬件的组装实践,毕业论文-计算机硬件组装实践.doc
  4. 045_引用分类和WeakHashMap
  5. 021_jdbc-mysql入门
  6. 017_CSS长度单位
  7. rust高级矿场_高级 Rust 所有权管理
  8. Spring原理简述
  9. v-for中为什么要有key属性
  10. c语言hellowwo所占字节数,哪个懂C语言?帮忙做~个题,跪求