题目链接
递归遍历
思路:递归参数为树的根节点和存储元素的数组,无需返回值;递归结束条件为当前节点为空;单层递归逻辑为按照要求打遍历顺序,遍历左右子树和当前节点。
前序:
/**
* 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 traversal(TreeNode* root, vector<int>& nodes) {
if (!root) return;
nodes.push_back(root->val);
traversal(root->left, nodes);
traversal(root->right, nodes);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
traversal(root, res);
return res;
}
};
中序:
/**
* 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 traverse(TreeNode* root, vector<int>& nodes) {
if (!root) return;
traverse(root->left, nodes);
nodes.push_back(root->val);
traverse(root->right, nodes);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
traverse(root, res);
return res;
}
};
后序:
/**
* 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 traverse(TreeNode* root, vector<int>& nodes) {
if (!root) return;
traverse(root->left, nodes);
traverse(root->right, nodes);
nodes.push_back(root->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
traverse(root, res);
return res;
}
};
迭代遍历
前序:由于前序遍历顺序为中左右,即当前节点->左子树->右子树,因此访问的元素和要处理的元素顺序是一致的,每次访问的元素即为需处理(遍历,加入result
数组)的元素,可参考深度优先搜索,用栈保存访问过的元素。每轮迭代都弹出栈顶元素加入result
,并将该节点的左右子节点压入栈。
/**
* 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) {
stack<TreeNode*> stk;
vector<int> result;
if (root) stk.push(root);
while (!stk.empty()) {
TreeNode* cur = stk.top();
result.push_back(cur->val);
stk.pop();
if (cur->right) stk.push(cur->right);
if (cur->left) stk.push(cur->left);
}
return result;
}
};
中序:由于中序遍历顺序为左子树->当前节点->右子树,处理顺序和访问顺序不一致,每次访问的元素不等于需要处理的元素,即无法直接弹出栈顶加入result
。因此,需要用一个指针来访问节点,而栈用于保存需处理的节点。
/**
* 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) {
stack<TreeNode*> stk;
vector<int> result;
TreeNode* cur = root;
while (cur || !stk.empty()) {
if (cur) {
stk.push(cur);
cur = cur->left;
} else {
cur = stk.top();
stk.pop();
result.push_back(cur->val);
cur = cur->right;
}
}
return result;
}
};
后序:访问顺序左->右->中,即中->右->左的倒序,因此可参考前序遍历先按中->右->左顺序遍历,再进行反向
/**
* 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) {
stack<TreeNode*> stk;
vector<int> result;
if (root) stk.push(root);
while(!stk.empty()) {
TreeNode* cur = stk.top();
result.push_back(cur->val);
stk.pop();
if (cur->left) stk.push(cur->left);
if (cur->right) stk.push(cur->right);
}
reverse(result.begin(), result.end());
return result;
}
};
统一迭代
思路:中序遍历和后序遍历无法直接和前序遍历统一,是由于访问顺序和处理顺序不一致。以中序遍历为例,若参考前序遍历的方法弹出栈顶x
进行访问,由于x
无法直接进行处理加入result
,因此需按x.right
、x
、x.left
的顺序压入栈,这样做无法区分栈中元素是否已访问过,也无法确定弹出的栈顶元素应进行访问还是处理。因此,为了统一迭代方式,可以对已经访问过的元素进行标记,这样,当下次弹出元素为已访问元素时,就可直接处理(加入result
)。标记方法:在弹出栈顶x
进行访问后,重新将x
压入栈时,多压入一个NULL
,这样下次弹出NULL
时,就知道x
已访问过,可直接弹出x
加入result
。
前序:
/**
* 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) {
stack<TreeNode*> stk;
vector<int> result;
if (root) stk.push(root);
while (!stk.empty()) {
TreeNode* cur = stk.top();
stk.pop();
if (cur) {
if (cur->right) stk.push(cur->right);
if (cur->left) stk.push(cur->left);
stk.push(cur);
stk.push(NULL);
} else {
result.push_back(stk.top()->val);
stk.pop();
}
}
return result;
}
};
中序:
/**
* 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) {
stack<TreeNode*> stk;
vector<int> result;
if (root) stk.push(root);
while (!stk.empty()) {
TreeNode* cur = stk.top();
stk.pop();
if (cur) {
if (cur->right) stk.push(cur->right);
stk.push(cur);
stk.push(NULL);
if (cur->left) stk.push(cur->left);
} else {
result.push_back(stk.top()->val);
stk.pop();
}
}
return result;
}
};
后序:
/**
* 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) {
stack<TreeNode*> stk;
vector<int> result;
if (root) stk.push(root);
while (!stk.empty()) {
TreeNode* cur = stk.top();
stk.pop();
if (cur) {
stk.push(cur);
stk.push(NULL);
if (cur->right) stk.push(cur->right);
if (cur->left) stk.push(cur->left);
} else {
result.push_back(stk.top()->val);
stk.pop();
}
}
return result;
}
};