二叉树遍历非递归程序 -- 使用栈模拟系统栈
【摘要】 一、前序遍历给定一个二叉树,返回它的 前序 遍历。示例:输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3]进阶: 递归算法很简单,你可以通过迭代算法完成吗? 前序遍历C++代码/** * Definition for a binary tree node. * struct TreeNode { * int val; * ...
一、前序遍历
给定一个二叉树,返回它的 前序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,2,3]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
前序遍历C++代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct Command
{
string s; // go, print
TreeNode* node;
Command(string s, TreeNode* node): s(s), node(node){}
};
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
if (root == NULL)
{
return res;
}
stack<Command> stack;
stack.push(Command("go", root));
while ( !stack.empty() )
{
Command command = stack.top();
stack.pop();
if (command.s == "print")
{
res.push_back(command.node -> val);
}
else
{
assert( command.s == "go");
if (command.node -> right)
{
stack.push(Command("go", command.node -> right));
}
if (command.node -> left)
{
stack.push(Command("go", command.node -> left));
}
stack.push(Command("print", command.node));
}
}
return res;
}
};
二、中序遍历
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
中序遍历C++代码
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct Command
{
string s; // go, print
TreeNode *node;
Command(string s, TreeNode *node) : s(s), node(node) {}
};
class Solution
{
public:
vector<int> inorderTraversal(TreeNode *root)
{
vector<int> res;
if (root == NULL)
{
return res;
}
stack<Command> stack;
stack.push(Command("go", root));
while (!stack.empty())
{
Command command = stack.top();
stack.pop();
if (command.s == "print")
{
res.push_back(command.node->val);
}
else
{
assert(command.s == "go");
if (command.node->right)
{
stack.push(Command("go", command.node->right));
}
stack.push(Command("print", command.node));
if (command.node->left)
{
stack.push(Command("go", command.node->left));
}
}
}
return res;
}
};
// @lc code=end
三、后序遍历
给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
后序遍历C++代码
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct Command
{
string s; // go, print
TreeNode *node;
Command(string s, TreeNode *node) : s(s), node(node) {}
};
class Solution
{
public:
vector<int> postorderTraversal(TreeNode *root)
{
vector<int> res;
if (root == NULL)
{
return res;
}
stack<Command> stack;
stack.push(Command("go", root));
while (!stack.empty())
{
Command command = stack.top();
stack.pop();
if (command.s == "print")
{
res.push_back(command.node->val);
}
else
{
assert(command.s == "go");
stack.push(Command("print", command.node));
if (command.node->right)
{
stack.push(Command("go", command.node->right));
}
if (command.node->left)
{
stack.push(Command("go", command.node->left));
}
}
}
return res;
}
};
// @lc code=end
练习题: 341.Flatten Nested List Iterator
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)