二叉树 实现一个二叉查找树,并且支持插入、删除、查找操作 实现查找二叉查找树中某个节点的后继、前驱节点 实现二叉树前、中、后序以及按层遍历 并完成leetcode上的验证二叉搜索树(98)及二叉树 层次遍历(102,107)!(选做)(保留往期第四天任务)注:这个跟下面的习题有重复 堆 实现一个小顶堆、大顶堆、优先级队列 实现堆排序 利用优先级队列合并 K 个有序数组 求一组动态数据集合的最大 Top K (选做)第三天堆排序学习(复习)
- 前序遍历
前序遍历首先访问根节点,然后遍历左子树,最后遍历右子树
使用递归:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int>res;
vector<int> preorderTraversal(TreeNode* root) {
if(root)
{
res.push_back(root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
return res;
}
};
- 中序遍历
中序遍历是先遍历左子树,然后访问根节点,然后遍历右子树。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int>res;
vector<int> inorderTraversal(TreeNode* root) {
if(root)
{
inorderTraversal(root->left);
res.push_back(root->val);
inorderTraversal(root->right);
}
return res;
}
};
- 后序遍历
先遍历左子树,然后遍历右子树,最后访问根节点
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int>res;
vector<int> postorderTraversal(TreeNode* root) {
if(root)
{
postorderTraversal(root->left);
postorderTraversal(root->right);
res.push_back(root->val);
}
return res;
}
};
层序遍历
层序遍历就是逐层遍历树结构
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root)
{
vector<int>res2;
res2.push_back(root->val);
res.push_back(res2);
vector<TreeNode*> node;
node.push_back(root);
int last=1;
int accur=0;
while(accur<node.size())
{
last=node.size();
vector<int> res1;
int res3=0;
while(accur<last)
{
if(node[accur]->left)
{
node.push_back(node[accur]->left);
res1.push_back(node[accur]->left->val);
res3++;
}
if(node[accur]->right)
{
node.push_back(node[accur]->right);
res1.push_back(node[accur]->right->val);
res3++;
}
accur++;
}
if(res3!=0)
res.push_back(res1);
}
}
return res;
}
};
- 二叉树的最大深度
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
return root ? max(maxDepth(root->left), maxDepth(root->right)) + 1 : 0;
}
};
- 路径之和
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(root==NULL)
{
return false;
}
int t= sum-root->val;
if(root->left==NULL && root->right==NULL)
return t==0 ? true:false;
return hasPathSum(root->left,t) || hasPathSum(root->right,t);
}
};