[LeetCode 题解]: Binary Tree Preorder Traversal
前言
【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相关推荐
- 【二叉树迭代版前序遍历】LeetCode 144. Binary Tree Preorder Traversal
LeetCode 144. Binary Tree Preorder Traversal Solution1:递归版 二叉树的前序遍历递归版是很简单的,前序遍历的迭代版相对是最容易理解的. 迭代版链接 ...
- [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 ...
- leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- Leetcode - 144. Binary Tree Preorder Traversal (层次遍历)
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- [LeetCode] 144. Binary Tree Preorder Traversal Java
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given bina ...
- LeetCode 145. Binary Tree Postorder Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-postorder-traversal/ 题目: Given a binary tree, retu ...
- LeetCode 144. Binary Tree Preorder Traversal--二叉树前序遍历--反向压栈--迭代-栈,递归--C++,Python解法
题目地址:Binary Tree Preorder Traversal - LeetCode Given a binary tree, return the preorder traversal of ...
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode* ...
最新文章
- HTML5音乐播放器(四):播放列表与播放方式
- 创建一个dynamics CRM workflow (三) - Creating Configuration Entity for Custom Workflow
- 【体验】ESP32-CAM可能是最便宜的“监控”方案,ESP32-CAM程序下载调试
- 近似最近邻搜索ANN(Approximate Nearest Neighbor)
- python中shutil模块的用法_python中的os,shutil模块的定义以及用法
- python中的format方法和int方法
- 首次开源!一行代码轻松搞定中英文语音识别、合成、翻译核心功能!
- (四)DOM对象和jQuery对象
- 宜创科技:低代码技术赋能企业数字化
- 知行之桥®中文版EDI系统正式发布
- GPS-GGA数据格式
- 怎么复制豆丁网的文字
- 火车票电子客票系统已全面上线,如何识别多种身份有效证件?
- 智能名片小程序名片详情页功能实现关键代码
- 有别于普通专线的BGP线路
- GROMACS .mdp 选项翻译及笔记
- 愿得一人心——祭奠······埋葬我218的爱情
- Python基础入门自学——18--操作Excel-工作实践需求
- 数据库作业17:第六章总结
- 创业计划书和商品计划书PPT模板-朴尔PPT