ACM模板列表 https://blog.csdn.net/nameofcsdn/article/details/107235360
二叉树:
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) {}
};
常用操作:
(1)深度相关
//求二叉树的深度
//二叉树的最小深度(最短路径上的节点数量)
//求二叉树的深度
int maxDepth(TreeNode* root) {
if (!root)return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
//minDepth辅助函数
int minDepth2(TreeNode* root) {
if (!root)return 1234567890;
if (!root->left && !root->right)return 1;
return min(minDepth2(root->left), minDepth2(root->right)) + 1;
}
//二叉树的最小深度(最短路径上的节点数量)
int minDepth(TreeNode* root) {
if (!root)return 0;
return minDepth2(root);
}
(2)二叉树的遍历
//二叉树的前序遍历
//二叉树的后序遍历
//二叉树的中序遍历
//根据前序和中序重建二叉树,假设没有重复数字
//根据中序和后序重建二叉树,假设没有重复数字
//二叉树的前序遍历
vector preorderTraversal(TreeNode* root) {
vectorv1;
if (root == NULL)return v1;
v1.insert(v1.end(), root->val);
vectorv2 = preorderTraversal(root->left);
v1.insert(v1.end(), v2.begin(), v2.end());
v2 = preorderTraversal(root->right);
v1.insert(v1.end(), v2.begin(), v2.end());
return v1;
}
//二叉树的后序遍历
vector postorderTraversal(TreeNode* root) {
vectorv1;
if (root == NULL)return v1;
vectorv2 = postorderTraversal(root->left);
v1.insert(v1.end(), v2.begin(), v2.end());
v2 = postorderTraversal(root->right);
v1.insert(v1.end(), v2.begin(), v2.end());
v1.insert(v1.end(), root->val);
return v1;
}
//二叉树的中序遍历
vector inorderTraversal(TreeNode* root) {
vectorv1;
if (root == NULL)return v1;
v1 = inorderTraversal(root->left);
v1.insert(v1.end(), root->val);
vectorv2 = inorderTraversal(root->right);
v1.insert(v1.end(), v2.begin(), v2.end());
return v1;
}
//辅助函数
TreeNode* buildTree(vector& preorder,int s1, vector& inorder, int s2, int len) {
if(len<=0)return NULL;
TreeNode *ans=new TreeNode;
ans->val=preorder[s1];
auto loc=find(inorder.begin()+s2,inorder.begin()+s2+len,preorder[s1]);
ans->left=buildTree(preorder,s1+1,inorder,s2,loc-inorder.begin()-s2);
ans->right=buildTree(preorder,loc-inorder.begin()-s2+s1+1,inorder,loc-inorder.begin()+1,len-(loc-inorder.begin()-s2)-1);
return ans;
}
//根据前序和中序重建二叉树,假设没有重复数字
TreeNode* buildTree(vector& preorder, vector& inorder) {
return buildTree(preorder,0,inorder,0,inorder.size());
}
//辅助函数
TreeNode* buildTree2(vector& postorder,int s1, vector& inorder,int s2, int len) {
if(len<=0)return NULL;
TreeNode *ans=new TreeNode;
ans->val=postorder[s1+len-1];
auto loc=find(inorder.begin()+s2,inorder.begin()+s2+len,postorder[s1+len-1]);
ans->left=buildTree2(postorder,s1,inorder,s2,loc-inorder.begin()-s2);
ans->right=buildTree2(postorder,loc-inorder.begin()-s2+s1,inorder,loc-inorder.begin()+1,len-(loc-inorder.begin()-s2)-1);
return ans;
}
//根据中序和后序重建二叉树,假设没有重复数字
TreeNode* buildTree2(vector& inorder, vector& postorder) {
return buildTree2(postorder,0,inorder,0,inorder.size());
}
(3)其他
//求节点个数
//拷贝二叉树
//判断两棵二叉树是否全等
//翻转二叉树
//求节点个数
int countNodes(TreeNode* root) {
if (!root)return 0;
return countNodes(root->left) + countNodes(root->right) + 1;
}
//拷贝二叉树
TreeNode* copyTree(TreeNode* root)
{
if(!root)return root;
return new TreeNode(root->val,copyTree(root->left),copyTree(root->right));
}
//判断两棵二叉树是否全等
bool isSameTree(TreeNode* r1,TreeNode* r2)
{
if(r1==NULL && r2==NULL)return true;
if(r1==NULL || r2==NULL)return false;
if(r1->val!=r2->val)return false;
return isSameTree(r1->left,r2->left)&&isSameTree(r1->right,r2->right);
}
//翻转二叉树
TreeNode* invertTree(TreeNode* root) {
if(!root)return root;
TreeNode *p=root->left,*q=root->right;
root->left=q,root->right=p;
invertTree(p);
invertTree(q);
return root;
}