1、叉树的层次遍历
/**
* 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> levelOrder(TreeNode* root) {
vector> res;
queue q;
if(root) q.push(root);
while(q.size())
{
vector level;
int len = q.size();
for(int i = 0;i < len;i ++) {
TreeNode* t = q.front();
q.pop();
level.push_back(t->val);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
res.push_back(level);
}
return res;
}
};
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 res;
void dfs(TreeNode* root) {
if(root) {
res.push_back(root->val);
dfs(root->left);
dfs(root->right);
}
}
vector preorderTraversal(TreeNode* root) {
dfs(root);
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:
vector preorderTraversal(TreeNode* root) {
vector res;
TreeNode* p = root;
stack stk;
while(p || stk.size())
{
while(p) {
res.push_back(p->val);
stk.push(p);
p = p->left;
}
p = stk.top()->right;
stk.pop();
}
return res;
}
};
3、中序遍历
递归
/**
* 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 res;
void dfs(TreeNode* root)
{
if(root) {
dfs(root->left);
res.push_back(root->val);
dfs(root->right);
}
}
vector inorderTraversal(TreeNode* root) {
dfs(root);
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:
vector inorderTraversal(TreeNode* root) {
vector res;
stack stk;
TreeNode* p = root;
while(p || stk.size())
{
while(p) {
stk.push(p);
p = p->left;
}
p = stk.top();
stk.pop();
res.push_back(p->val);
p = p->right;
}
return res;
}
};
4、后续遍历
递归
/**
* 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 res;
void dfs(TreeNode* root)
{
if(root)
{
dfs(root->left);
dfs(root->right);
res.push_back(root->val);
}
}
vector postorderTraversal(TreeNode* root) {
dfs(root);
return res;
}
};
迭代
后续遍历的做法如下:
1、前序遍历的顺序:根,左,右
2、可以根据类型前序遍历的思想,遍历出:根,右,左
3、再通过 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 postorderTraversal(TreeNode* root) {
vector res;
stack stk;
TreeNode* p = root;
while(p || stk.size())
{
while(p) {
res.push_back(p->val);
stk.push(p);
p = p->right;
}
p = stk.top()->left;
stk.pop();
}
reverse(res.begin(), res.end());
return res;
}
};
5、前序遍历 + 中序遍历构建二叉树
/**
* 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:
unordered_map pos;
TreeNode* build(vector& preorder, vector& inorder, int pl, int pr, int il, int ir)
{
if(pl > pr || il > ir) return NULL;
TreeNode* root = new TreeNode(preorder[pl]);
int k = pos[preorder[pl]];
int len = k - il;
root->left = build(preorder, inorder, pl + 1, pl + len, il, k - 1);
root->right = build(preorder, inorder, pl + len + 1, pr, k + 1, ir);
return root;
}
TreeNode* buildTree(vector& preorder, vector& inorder) {
for(int i = 0 ;i < preorder.size();i ++) pos[inorder[i]] = i;
return build(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
}
};
6、中序遍历 + 后序遍历构建二叉树
/**
* 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:
unordered_map pos;
TreeNode* build(vector postorder, vector& inorder, int pl, int pr, int il, int ir)
{
if(pl > pr || il > ir) return NULL;
TreeNode* root = new TreeNode(postorder[pr]);
int k = pos[postorder[pr]];
int len = k - il;//len为前序的长度
root->left = build(postorder, inorder, pl, pl + len - 1, il, k - 1);
root->right = build(postorder, inorder, pl + len, pr - 1, k + 1, ir);
return root;
}
TreeNode* buildTree(vector& inorder, vector& postorder) {
for(int i = 0;i < inorder.size();i ++) pos[inorder[i]] = i;
return build(postorder, inorder, 0, postorder.size() - 1, 0, inorder.size() - 1);
}
};