前序遍历
/**
* 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> result;
read(root,result);
return result;
}
void read(TreeNode* node, vector<int>& result){
if(node!=NULL){
result.push_back(node->val);
read(node->left,result);
read(node->right,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) {
vector<int> result;
read(root,result);
return result;
}
void read(TreeNode* node, vector<int>& result){
if(node!=NULL){
read(node->left,result);
result.push_back(node->val);
read(node->right,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) {
vector<int> result;
read(root,result);
return result;
}
void read(TreeNode* node, vector<int>& result){
if(node!=NULL){
read(node->left,result);
read(node->right,result);
result.push_back(node->val);
}
}
};
迭代遍历可以写得很不一样,因为不同的遍历方式中节点出栈入栈的条件不同,比如在前序遍历中,节点第一次被访问时就可以出栈,并将子节点入栈,在中序遍历中,则需要根据栈顶节点是否有左邻接节点做入栈,根据是否有右节点做出栈。统一迭代遍历通过NULL对要出栈进行读取的节点进行标记,使得代码得以统一。统一迭代遍历中,没有NULL标记的节点出栈后访问子节点,根据不同的遍历种类选择不同的入栈顺序,并使用NULL标记,有NULL标记的节点则出栈后访问val即可。
统一迭代前序遍历
/**
* 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*> st;
vector<int>result;
if(root==NULL) return result;
st.push(root);
while(!st.empty()){
TreeNode* node=st.top();
st.pop();
if(node!=NULL){
if(node->right!=NULL) st.push(node->right);
if(node->left!=NULL) st.push(node->left);
st.push(node);
st.push(NULL);
}else{
result.push_back(st.top()->val);
st.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*> st;
vector<int>result;
if(root==NULL) return result;
st.push(root);
while(!st.empty()){
TreeNode* node=st.top();
st.pop();
if(node!=NULL){
if(node->right!=NULL) st.push(node->right);
st.push(node);
st.push(NULL);
if(node->left!=NULL) st.push(node->left);
}else{
result.push_back(st.top()->val);
st.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*> st;
vector<int>result;
if(root==NULL) return result;
st.push(root);
while(!st.empty()){
TreeNode* node=st.top();
st.pop();
if(node!=NULL){
st.push(node);
st.push(NULL);
if(node->right!=NULL) st.push(node->right);
if(node->left!=NULL) st.push(node->left);
}else{
result.push_back(st.top()->val);
st.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<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>>result;
if(root==NULL) return result;
queue<pair<TreeNode*,int>>q;
q.push({root,0});
result.push_back(vector<int>{});
while(!q.empty()){
pair<TreeNode*,int>node=q.front();
q.pop();
if(node.first->left!=NULL) q.push({node.first->left,node.second+1});
if(node.first->right!=NULL) q.push({node.first->right,node.second+1});
result[node.second].push_back(node.first->val);
if(node.second<q.front().second) result.push_back(vector<int>{});
}
return result;
}
};