数据结构与算法之二叉树
文章目录
二叉树分类
二叉树主要分为:满二叉树和完全二叉树。
满二叉树:每一层结点都是满的,最终整棵树的结点总数为 2^k - 1。
完全二叉树:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 k 层,则该层包含 1~ 2k 个节点。
二叉树实现
二叉树单个结点
二叉树构建父节点与子节点之间的联系主要是通过指针来实现的。在结点结构中有一个val用来保存结点的值,left指针指向左子树,right指针指向右子树。结点结构代码如下:
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、二叉树的前序遍历
前序遍历:先遍历根结点,再遍历左子树,最后遍历右子树
(1)迭代实现:注意栈是先进后出结构,所以应先将右子树入栈,再将左子树入栈
vector<int> preorderTraversal(TreeNode* root){
if(root == nullptr)
return {};
stack<TreeNode*> stk;
vector<int> res;
stk.push(root);
while(!stk.empty()){
TreeNode* cur = stk.top();
stk.pop();
res.push_back(cur->val);
if(cur->right != nullptr)
stk.push_back(cur->right);
if(cur->left != nullptr)
stk.push_back(cur->left);
}
return res;
}
(2)递归实现
vector<int> preorderTraversal(TreeNode* root){
vector<int> res;
preorderRecursion(root, res);
return res;
}
void preorderRecursion(TreeNode* root, vector<int>& res){
if(root == nullptr)
return;
res.push_back(root->val);
preorderRecursion(root->left, res);
preorderRecursion(root->right, res);
}
2、二叉树的中序遍历
中序遍历:先遍历左子树,再遍历根结点,最后遍历右子树
(1)迭代实现:
vector<int> inorderTraversal(TreeNode* root){
stack<TreeNode*> stk;
vector<int> res;
TreeNode* cur = root;
while(!stk.empty() || cur != nullptr){
if(cur != nullptr){
stk.push(cur);
cur = cur->left;
}else{
cur = stk.top();
res.push_back(cur->val);
cur = cur->right;
}
}
return res;
}
(2)递归实现
vector<int> inorderTraversal(TreeNode* root){
vector<int> res;
inorderRecursion(root, res);
return res;
}
void inorderRecursion(TreeNode* root, vector<int>& res){
if(root == nullptr)
return;
inorderRecursion(root->left, res);
res.push_back(root->val);
inorderRecursion(root->right, res);
}
3、二叉树的后序遍历
后序遍历:先遍历左子树,再遍历右子树,最后遍历根结点
下面将用两中不同思路实现后序遍历
(1.1)迭代实现:
后序遍历可以通过将前序遍历变形得到:中左右(前序遍历)->中右左->左右中。
要根据前序遍历得到中右左的遍历顺序,只需要先将左子树入栈,再将右子树入栈。
要根据中右左的遍历顺序得到左右中的遍历顺序,只需要将最终结果进行反转。
vector<int> postorderTraversal(TreeNode* root){
if(root == nullptr)
return;
stack<TreeNode*> stk;
vector<int> res;
stk.push(root);
while(!stk.empty()){
TreeNode* cur = stk.top();
stk.pop();
res.push_back(cur->val);
//这里与前序遍历不同
if(cur->left != nullptr){
stk.push_back(cur->left);
}
if(cur->right != nullptr){
stk.push_back(cur->right);
}
}
reverse(res.begin(), res.end()); //反转res
return res;
}
(1.2)迭代实现:
vector<int> postorderTraversal(TreeNode* root){
if(root == nullptr)
return;
stack<TreeNode*> stk;
TreeNode* cur = root;
TreeNode* last = nullptr;//保存上一个结点
vector<int> res;
while(!stk.empty() || cur != nullptr){
if(cur != nullptr){
stk.push(cur);
cur = cur->left;
}else{
cur = stk.top();
stk.pop();
if(cur->right == nullptr || cur->right == last){
res.push_back(cur->val);
last = cur;
cur = nullptr;
}else{
stk.push(cur);
cur = cur->right;
}
}
}
return res;
}
(2)递归实现
vector<int> postorderTraversal(TreeNode* root){
vector<int> res;
postorderRecursion(root, res);
return res;
}
void postorderRecursion(TreeNode* root, vector<int>& res){
if(root == nullptr)
return;
postorderRecursion(root->left, res);
postorderRecursion(root->right, res);
res.push_back(root->val);
}
3、二叉树的层序遍历
层序遍历:从第一层开始遍历,直达遍历完所有结点
vector<vector<int>> levelOrder(TreeNode* root){
if(root == nullptr)
return;
vector<int> res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int s = q.size();
vector<int> temp;
for(int i = 0; i < s; ++i){
TreeNode* cur = q.front();
q.pop();
temp.push_back(cur->val);
if(cur->left != nullptr)
q.push(cur->left);
if(cur->right != nullptr)
q.push(cur->right);
}
res.push_back(temp);
}
return res;
}
构建二叉树
一般构建二叉树必须要知道其中序遍历结果,而前序遍历结果和后序遍历结果只要知道其中之一。
1、根据中序遍历及前序遍历构建二叉树
前序遍历结果保存顺序为:根节点,左子树结点,右子树结点
中序遍历结果保存顺序为:左子树结点,根节点,右子树结点
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder){
if(preorder.size() == 0 || inorder.size() == 0)
return nullptr;
return buildTreeTraversal(preorder, 0, preorder.size(), inorder, 0, inorder.size());
}
//区间遵循左闭右开原则
TreeNode* buildTreeTraversal(vector<int>& preorder, int preorderBegin, int preorderEnd, vector<int>& inorder, int inorderBegin, int inorderEnd){
if(preorderBegin == preorderEnd)
return nullptr;
int rootVal = preorder[preorderBegin]; //前序遍历的preorderBegin索引处一定为根结点
TreeNode* root = new TreeNode(rootVal);
if(preorderEnd - preorderBegin == 1)
return root;
//在中序遍历结果中先找到中间结点
int index = 0;
for(index; index < inorder.size(); ++index){
if(inorder[index] == rootVal)
break;
}
//分割中序数组
//中序数组的左子树为[inorderBegin, index)
int inorderLeftBegin = inorderBegin;
int inorderLeftEnd = index;
//中序数组的右子树范围为[index + 1, inorderEnd)
int inorderRightBegin = index + 1;
int inorderRightEnd = inorderEnd;
//分割前序数组
//前序数组的左子树为[preorderBegin + 1, preorderBegin + 1 + (index - inorderBegin)),其中index - inorderBegin为左子树结点个数
int preorderLeftBegin = preorderBegin + 1;
int preorderLeftEnd = preorderLeftBegin + (index - inorderBegin);//加上左子树的结点数
//前序数组的右子树[preorderLeftEnd, preorderEnd)
int preorderRightBegin = preorderLeftEnd;
int preorderRightEnd = preorderEnd;
//构建左子树
root->left = buildTreeTraversal(preorder, preorderLeftBegin, preorderLeftEnd, inorder, inorderLeftBegin, inorderLeftEnd);
//构建右子树
root->right = buildTreeTraversal(preorder, preorderRightBegin, preorderRightEnd, inorder, inorderRightBegin, inorderRightEnd);
return root;
}
2、根据中序遍历及后序遍历构建二叉树
后序遍历结果保存顺序为:左子树结点,右子树结点,根结点
中序遍历结果保存顺序为:左子树结点,根节点,右子树结点
TreeNode* buildTree(vector<int>& postorder, vector<int>& inorder){
}
TreeNode* buildTreeTraversal(vector<int>& postorder, int postorderBegin, int postorderEnd, vector<int>& inorder, int inorderBegin, int inorderEnd){
if(postorderBegin == postorderEnd)
return nullptr;
int rootVal = postorder[postorderEnd - 1];//后序遍历数组的最后一个结点一定为根结点
TreeNode* root = new TreeNode(rootVal);
if(postorderEnd - postorderBegin == 1)
return root;
//在中序数组中找根结点
int index = 0;
for(index; index < inorder.size(); ++index){
if(inorder[index] = rootVal)
break;
}
//分割数组
//分割中序数组
//中序数组的左子树为[inorderBegin, index)
int inorderLeftBegin = inorderBegin;
int inorderLeftEnd = index;
//中序数组的右子树范围为[index + 1, inorderEnd)
int inorderRightBegin = index + 1;
int inorderRightEnd = inorderEnd;
//分割后序数组
//后序数组的左子树[postorderBegin, postorderBegin + (index - inorderLeftBegin))其中(index - inorderLeftBegin)是左子树的结点个数
int postorderLeftBegin = postorderBegin;
int postorderLeftEnd = postorderLeftBegin + (index - inorderLeftBegin);
//后序数组的右子树[postorderLeftEnd + 1, postorderEnd)
int postorderRightBegin = postorderLeftEnd + 1;
int postorderRightEnd = postorderEnd - 1;
//构建左子树
root->left = buildTreeTraversal(postorder, postorderLeftBegin, postorderLeftEnd, inorder, inorderLeftBegin, inorderLeftEnd);
//构建右子树
root->right = buildTreeTraversal(postorder, postorderRightBegin, postorderRightEnd, inorder, inorderRightBegin, inorderRightEnd);
return root;
}
二叉树的深度
(1)迭代法实现
int TreeDepth(TreeNode* root){
if(root == nullptr)
return 0;
int depth = 0;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int s = q.size();
for(int i = 0; i < s; ++i){
TreeNode* cur = q.front();
q.pop();
if(cur->left != nullptr)
q.push(cur->left);
if(cur->right != nullptr)
q.push(cur->right);
}
++depth;
}
return depth;
}
(2)递归实现
int TreeDepth(TreeNode* root){
if(root == nullptr)
return 0;
int leftDept = TreeDepth(root->left) + 1;
int rightDept = TreeDepth(root->right) + 1;
return (left >= right ? left + 1 : right);
}
总结
1、二叉树的前、中、后序遍历方式属于深度优先搜索。
2、二叉树的层序遍历属于广度优先搜索。
3、在构造二叉树时,一定要知道其中序遍历结果,首先通过前序(后序)遍历结果可以确定根节点,再利用中序遍历结果可以找到根节点位置,则自然就将树分为了左子树和右子树。