C++中map、set、multimap,multiset的底层实现都是平衡二叉搜索树,所以map、set的增删操作时间时间复杂度是logn,注意我这里没有说unordered_map、unordered_set,unordered_map、unordered_set底层实现是哈希表。
这句话来自代码随想录,没看懂,希望二刷能搞明白。
递归
/**
* 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 mpreturn(TreeNode* root , vector<int>& res )
{
if(root == NULL)
{
return;
}
res.push_back(root->val);
mpreturn(root->left , res);
mpreturn(root-> right, res);
}
vector<int> preorderTraversal(TreeNode* root) {
vector <int> res ;
mpreturn (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:
vector<int> preorderTraversal(TreeNode* root) {
vector <int> res ;
stack <TreeNode*> temp;
temp.push(root);
if(root == NULL)
{
return res;
}
while(!temp.empty())
{
root = temp.top();
res.push_back(root -> val);
temp.pop();
if(root -> right != NULL) temp.push(root -> right);
if(root -> left != NULL) temp.push(root -> left);
}
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 mpreturn(TreeNode* root , vector<int>& res)
{
if(root -> left != NULL) mpreturn(root->left, res);
if(root -> right != NULL)mpreturn(root -> right , res);
if(root == NULL)
{
return;
}
res.push_back(root -> val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if(root == NULL)
{
return res;
}
mpreturn(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:
vector<int> postorderTraversal(TreeNode* root) {
vector <int> res ;
stack <TreeNode*> temp;
temp.push(root);
if(root == NULL)
{
return res;
}
while(!temp.empty())
{
root = temp.top();
res.push_back(root -> val);
temp.pop();
if(root -> left != NULL) temp.push(root -> left);
if(root -> right != NULL) temp.push(root -> right);
}
reverse(res.begin(), res.end());
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 myreturn (TreeNode* root , vector<int>& res)
{
if(root == NULL)
{
return;
}
myreturn(root->left , res);
res.push_back(root ->val);
myreturn(root->right , res);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
myreturn(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:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack <TreeNode*> temp;
while(!temp.empty() || root != NULL)
{
if(root != NULL)
{
temp.push(root);
root = root -> left;
}
else
{
root = temp.top();
res.push_back(root -> val);
temp.pop();
root = root -> right;
}
}
return res;
}
};