Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1\2/3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
中序遍历非递归版本
法一:
class Solution { private:vector<int> res; public:vector<int> inorderTraversal(TreeNode *root) {res.clear();if(root==NULL) return res;stack<pair<TreeNode*,bool>> s;s.push(make_pair(root,false));while (!s.empty()){root=s.top().first;while (root!=NULL&&!s.top().second){s.top().second=true;root=root->left;if(root!=NULL) s.push(make_pair(root,false));}root=s.top().first->right;res.push_back(s.top().first->val);s.pop();if(root!=NULL)s.push(make_pair(root,false));}return res;} };
法二:
class Solution { private:vector<int> res; public:vector<int> inorderTraversal(TreeNode *root) {res.clear();if(root==NULL) return res;stack<pair<TreeNode*,bool>> s;TreeNode* t;int used;s.push(make_pair(root,false));while(!s.empty()){t=s.top().first;used = s.top().second;s.pop();if(!used){if(t->right!=NULL) s.push( make_pair(t->right,false));s.push(make_pair(t,true));if(t->left!=NULL) s.push( make_pair(t->left,false));}else res.push_back(t->val);}return res;} };
转载于:https://www.cnblogs.com/fightformylife/p/4244454.html
Binary Tree Inorder Traversal相关推荐
- LeetCode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...
- 【二叉树迭代版中序遍历】LeetCode 94. Binary Tree Inorder Traversal
LeetCode 94. Binary Tree Inorder Traversal Solution1:递归版 二叉树的中序遍历递归版是很简单的,中序遍历的迭代版需要特殊记一下! 迭代版链接:htt ...
- 15 二叉树的中序遍历(Binary Tree Inorder Traversal)
文章目录 1 题目 2 描述 3 解决方案 3.1 递归算法 3.1.1 遍历法(Traverse) 思路 源码 3.1.2 分治法(Devide And Conquer) 思路 源码 3.2 非递归 ...
- [LeetCode]:94:Binary Tree Inorder Traversal
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...
- LeetCode Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- leetcode[94]Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- [swift] LeetCode 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- 94. Binary Tree Inorder Traversal
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...
- LintCode: Binary Tree Inorder Traversal
C++,递归,辅助函数 1 /** 2 * Definition of TreeNode: 3 * class TreeNode { 4 * public: 5 * int val; 6 * Tree ...
最新文章
- Yarn将用TypeScript重写,Flow惨遭亲爹抛弃!
- UVA 1524 - Hot or Cold?(数学)
- 布法罗博士计算机专业回国人员,四名UW学生参加爱达荷州国家实验室的实习计划...
- 【中级05】Java泛型、反射
- Excel 如何使多组数据的饼状图大小一致
- 深圳大学计算机与软件学院自考,如何坐公交 或 地铁去深圳的深圳大学南校区计算机与软件学院自考办| Moovit...
- IBC 2019 五篇文章阅读笔记
- Arduino资源下载
- CCA (Canonical correlation analysis) 典型相关分析
- php命令行生成文件,php命令行生成与读取配置文件
- 【RFID】阅读器和应答器之间的电感耦合
- 揭秘nginx访问的神秘面纱
- 从红海里面寻找蓝海,看一个人的思维模式
- 2021前端面试经典计算题总结。
- PPT卡片排版技巧,快来收藏
- 弘辽科技:淘宝宝贝标题怎么找准关键词?做标题的3个学问
- 拥有它,XML文件少一半
- noip模拟题11.11 光棍节测试
- 浅谈赢得值理论的运用-Oracle p6
- 【转载】计算机视觉研究方向
热门文章
- python中的正则表达式re模块_python中的正则表达式(re模块)
- android 筛选控件_Flutter学习六之实现一个带筛选的列表页面
- 全球及中国木材加工行业运行状况与投资产值预测报告2022版
- 全球及中国润滑油市场产销规模及营销竞争分析报告2021-2027年
- iOS快速开发框架--Bee Framework
- MFC让文档/视图结构程序支持滚动条
- 智联招聘python岗位_智联招聘的python岗位数据词云制作
- 作业三——原型化系统——外卖app
- PHP 防XSS跨站攻击
- [luoguP2896] [USACO08FEB]一起吃饭Eating Together(DP)