Binary Tree Inorder Traversal 二叉树的中序遍历
2014-01-20 10:56
253 查看
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> vec;
stack<TreeNode*> s;
TreeNode *p=root;
while(p!=NULL||!s.empty())
{
while(p!=NULL)
{
s.push(p);
p=p->left;
}
if(!s.empty())
{
p=s.top();
s.pop();
vec.push_back(p->val);
p=p->right;
}
}
return vec;
}
};
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> vec;
stack<TreeNode*> s;
TreeNode *p=root;
while(p!=NULL||!s.empty())
{
while(p!=NULL)
{
s.push(p);
p=p->left;
}
if(!s.empty())
{
p=s.top();
s.pop();
vec.push_back(p->val);
p=p->right;
}
}
return vec;
}
};
相关文章推荐
- Binary Tree Inorder Traversal-非递归实现中序遍历二叉树
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
- Binary Tree Inorder Traversal 二叉树的中序遍历(C++:Iterative solution using stack)
- LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
- LeetCode OJ 之 Binary Tree Inorder Traversal (二叉树的中序遍历)
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- 【遍历二叉树】02二叉树的中序遍历【Binary Tree Inorder Traversal】
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
- LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal(从前序以及中序遍历结果中构造二叉树)
- leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)
- Leetcode - Tree - 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历重构二叉树)
- LeetCode:BInary Tree Inorder Traversal(二叉树的中序遍历)
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal (利用中序遍历和后续遍历确定一颗二叉树)
- LeetCode | Binary Tree Inorder Traversal(二叉树的中序遍历)
- LintCode Binary Tree Inorder Traversal 二叉树的中序遍历(非递归)
- lintcode 容易题:Binary Tree Inorder Traversal 二叉树的中序遍历
- [leetcode]Construct Binary Tree from Preorder and Inorder Traversal(根据前序、中序遍历确定一棵二叉树 C语言)
- Binary Tree Inorder Traversal 二叉树的中序遍历
- LeetCode 94 Binary Tree Inorder Traversal(二叉树的中序遍历)+(二叉树、迭代)
- 二叉树的中序遍历 Binary Tree Inorder Traversal