标签:tor 遍历 ble init || public c++ c++实现 leetcode
中序遍历二叉树(非递归)
使用一个辅助栈
题目来源
C++实现
/**
* Definition for a binary tree node.
* 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)
{
stack<TreeNode*> data;
vector<int> treedata;
while(!data.empty() || root != NULL)
{
if(root != NULL)
{
data.push(root);
root = root->left;
}
else
{
root = data.top();
treedata.push_back(root->val);
data.pop();
root = root->right;
}
}
return treedata;
}
};
94. Binary Tree Inorder Traversal
标签:tor 遍历 ble init || public c++ c++实现 leetcode
原文地址:https://www.cnblogs.com/Manual-Linux/p/12074696.html