1.二叉树前序遍历
模板:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
void order(TreeNode* root) {
if (root == NULL) {return;}
/*执行动作*/
order(root->left);
order(root->right);
}
class Solution {
public:
void order(TreeNode* root, vector<int> &ans){
if(root == NULL){return;}
ans.push_back(root->val);
order(root->left, ans);
order(root->right, ans);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ans;
order(root, ans);
return ans;
}
};
2.二叉树的中序遍历
模板:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
void order(TreeNode* root) {
if (root == NULL) {return;}
order(root->left);
/*执行动作*/
order(root->right);
}
class Solution {
public:
void order(TreeNode* root, vector<int> &ans){
if(root == NULL){return;}
order(root->left, ans);
ans.push_back(root->val);
order(root->right, ans);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ans;
order(root, ans);
return ans;
}
};
3.二叉树的后序遍历
模板:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
void order(TreeNode* root) {
if (root == NULL) {return;}
order(root->left);
order(root->right);
/*执行动作*/
}
class Solution {
public:
void order(TreeNode* root, vector<int> &ans){
if(root == NULL){return;}
order(root->left, ans);
ans.push_back(root->val);
order(root->right, ans);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ans;
order(root, ans);
return ans;
}
};
4.二叉树的层序遍历
模板:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
* /
void levelOrder(TreeNode* root) {
queue<TreeNode*> q;
if(root == nullptr){return ;}
q.push(root);
while(!q.empty())
{
int curSize = q.size();
for(int i = 0; i < curSize; i++){
TreeNode* node = q.front();
q.pop();
/*执行动作*/
if(node->left != nullptr){q.push(node->left);}
if(node->right != nullptr){q.push(node->right);}
}
}
}
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> q;
vector<vector<int>> res;
if(root == nullptr){return res;}
q.push(root);
while(!q.empty())
{
int curSize = q.size();
res.push_back(vector<int>());
for(int i = 0; i < curSize; i++){
TreeNode* node = q.front();
q.pop();
res.back().push_back(node->val);
if(node->left != nullptr){q.push(node->left);}
if(node->right != nullptr){q.push(node->right);}
}
}
return res;
}
};
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> res;
queue<TreeNode*> q;
if(root == nullptr){return res;}
q.push(root);
while(!q.empty()){
int curSize = q.size();
res.push_back(vector<int>());
for(int i = 0; i < curSize; i++){
TreeNode* node = q.front();
q.pop();
res.back().push_back(node->val);
if(node->left != nullptr){q.push(node->left);}
if(node->right != nullptr){q.push(node->right);}
}
}
reverse(res.begin(),res.end());
return res;
}
};
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> res;
queue<TreeNode*> q;
if(root == nullptr){return res;}
q.push(root);
bool f = false;
while(!q.empty()){
int curSize = q.size();
res.push_back(vector<int>());
for(int i = 0; i < curSize; i++){
TreeNode* node = q.front();
q.pop();
res.back().push_back(node->val);
if(node->left != nullptr){q.push(node->left);}
if(node->right != nullptr){q.push(node->right);}
}
if(f){
reverse(res.back().begin(),res.back().end());
}
f =!f;
}
return res;
}
};
5.提升练习
leetcode 988. 从叶结点开始的最小字符串
class Solution {
public:
void order(TreeNode *root, vector<string> &ansVec, string ans)
{
if (root->left == nullptr && root->right == nullptr) {
ans += root->val + 'a';
ansVec.push_back(ans);
return;
}
ans += root->val + 'a';
if (root->left != nullptr) {
order(root->left, ansVec, ans);
}
if (root->right != nullptr) {
order(root->right, ansVec, ans);
}
}
string smallestFromLeaf(TreeNode *root)
{
string ans;
if (root == nullptr) {
return ans;
}
vector<string> ansVec;
order(root, ansVec, ans);
for (int i = 0; i < ansVec.size(); i++) {
reverse(ansVec[i].begin(), ansVec[i].end());
}
sort(ansVec.begin(), ansVec.end());
return ansVec[0];
}
};