LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium
题目:Binary Tree Inorder Traversal
二叉树的中序遍历,和前序、中序一样的处理方式,代码见下:
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x), left(NULL),right(NULL) {}
};
vector<int> preorderTraversal(TreeNode *root) //非递归的中序遍历(用栈实现)
{
if (NULL == root) {
return vector<int>();
}
stack<TreeNode *> tree_stack;
vector<int> tree_vector;
TreeNode *pTemp = root;
while (pTemp || !tree_stack.empty()) {
while (pTemp) {
tree_stack.push(pTemp);
pTemp = pTemp->left;
}
if (!tree_stack.empty()) {
pTemp = tree_stack.top();
tree_vector.push_back(pTemp->val);
tree_stack.pop();
pTemp = pTemp->right;
}
}
return tree_vector;
}
LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium的更多相关文章
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)
题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard
题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
随机推荐
- oracle数据库分页总结
/* BEGIN CREATE TABLE APPUSER(IDS NUMBER(8), USERNAME VARCHAR2(20), PASSWORD VARCHAR2(20), CTIME DAT ...
- 测试Linux下tcp最大连接数限制
现在做服务器开发不加上高并发根本没脸出门,所以为了以后吹水被别人怼“天天提高并发,你自己实现的最高并发是多少”的时候能义正言辞的怼回去,趁着元旦在家没事决定自己写个demo搞一搞. 这个测试主要是想搞 ...
- FortiGate下视频会议等语音相关配置
关闭老的基于会话的alg机制(即删除session-helper中的SIP条目) config system session-helper delete 13 #删除sip end
- eclipse删除了文件,找回方法
本人通过eclipse在前段时间上传svn代码的时候,代码掉完了,导致的原因是:svn服务器上有有个一样的文件夹,只是大小写不同,但是svn会认为是一样的文件夹,导致svn[]判别不了传到哪个文件夹去 ...
- 515. Find Largest Value in Each Tree Row查找一行中的最大值
[抄题]: You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ ...
- cmp指令
cmp是比较指令,cmp的功能相当于减法指令,只是不保存结果.cmp指令执行后,将对标志寄存器产生影响.其他相关指令通过识别这些被影响的标志寄存器位来得知比较结果. cmp指令格式: cmp 操作对象 ...
- Spark官方文档翻译(一)~Overview
Spark官方文档翻译,有问题请及时指正,谢谢. Overview页 http://spark.apache.org/docs/latest/index.html Spark概述 Apache Spa ...
- Error running Tomcat8: Address localhost:xxxx is already in use
参考自: https://blog.csdn.net/huazhongkejidaxuezpp/article/details/41813683 第一步,命令提示符号,执行命令:netstat -an ...
- 在python学习时间过程中,你会不断发现需要解决的问题,更多需要连接未知,这时候到哪里去查阅资料呢?
1.safari online book https://www.safaribooksonline.com 知乎上有人问,送程序员什么礼物好,其中一个答案就是safari online.编程的英文书 ...
- 4412 uboot启动分析
感谢sea1105, https://blog.csdn.net/sea1105/article/details/52142772 在学习过程中,由于tiny4412资料太过于少,因此参考210的视屏 ...