144. 二叉树的前序遍历
这题很简单,递归代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void preorder(TreeNode *root, vector<int> &res)
{
if (root == nullptr)
{
return;
}
res.push_back(root->val);
preorder(root->left, res);
preorder(root->right, res);
}
vector<int> preorderTraversal(TreeNode *root)
{
vector<int> res;
preorder(root, res);
return res;
}
};
但是这道题我们的目的是用迭代做出来
思路
1.先访问左路结点
2.再访问左路结点的右子树
这里访问右子树要以循环从栈依次取出这些结点,循环子问题的思想访问左路结点的右子树。
(中序和后序跟前序的思路完全一 致,只是前序先访问根还是后访问根的问题。后序稍微麻烦- -些,因为后序遍历的顺序是左子树右子树根,当取到左路结点的右子树时,需要想办法标记判断右子树是否访问过了,如果访问过了,就直接访问根,如果访问过需要先访问右子树
代码示例:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root)
{
vector<int> res;
if (root == nullptr)
{
return res;
}
stack<TreeNode*> stk;
TreeNode* cur = root;
while (!stk.empty() || cur)
{
//每次循环开始代码访问一棵树的开始
//访问左路节点,左路节点入栈
while (cur)
{
res.push_back(cur->val);
stk.push(cur);
cur = cur->left;
}
//取一个左路节点的右子树出来访问
cur = stk.top();
stk.pop();
cur = cur->right;
}
return res;
}
};
94. 二叉树的中序遍历
这题是上面那题的姊妹题,换换顺序就能写好了
迭代写法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root)
{
vector<int> res;
if (root == nullptr)
{
return res;
}
stack<TreeNode*> stk;
TreeNode* cur = root;
while (!stk.empty() || cur)
{
while (cur)
{
stk.push(cur);
cur = cur->left;
}
cur = stk.top();
stk.pop();
res.push_back(cur->val);
cur = cur->right;
}
return res;
}
};
145. 二叉树的后序遍历
一样姊妹题:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if (root == nullptr)
{
return res;
}
stack<TreeNode*> stk;
TreeNode* cur = root;
TreeNode* prev = nullptr;
while (!stk.empty() || cur)
{
while (cur)
{
stk.push(cur);
cur = cur->left;
}
cur = stk.top();
stk.pop();
if(cur->right==nullptr || cur->right == prev)
{
res.push_back(cur->val);
prev = cur;
cur = nullptr;
}
else
{
stk.push(cur);
cur=cur->right;
}
}
return res;
}
};