binary tree
Binary tree traversal
A full binary tree has only nodes with a degree of 0 or 2, and nodes with a degree of 0 are on the same layer; the depth of a full binary tree is k, and the number of nodes is 2^k - 1; a complete binary tree is not filled except for the
bottom node , the number of nodes in the remaining layers has reached the maximum value, and the nodes in the bottom layer are all concentrated in the leftmost position of the layer; the
priority queue is actually a heap, and the heap is a complete binary tree, while ensuring the order of the parent and child nodes relation.
Binary search tree
Balanced binary search tree (AVL) : It is an empty tree or the absolute value of the height difference between its left and right subtrees does not exceed 1, and both left and right subtrees are a balanced binary tree.
The underlying implementations of map, set, multimap, and multiset in C++ are all balanced binary trees, so the complexity of the addition and deletion operations of map and set is logn, but the underlying implementation of unordered_map and unordered_set is the storage method of
hash table binary tree , chain storage, order The storage array stores the binary tree, the subscript of the parent node is i, the left child i 2 + 1 the right child i 2 + 2; depth-first traversal (front, middle, post-order traversal) breadth-first traversal (level order traversal)
//前序遍历 递归
#include <iostream>
#include <vector>
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode() : val(), left(NULL), right(NULL) {
}
TreeNode(int x) : val(x), left(NULL), right(NULL) {
}
};
class Solution {
public:
void traversal(TreeNode* cur, std::vector<int>& v) {
if (cur == NULL) {
return;
}
//中序、后序遍历调整位置即可
v.push_back(cur->val); // 中
traversal(cur->left, v); // 左
traversal(cur->right, v); // 右
}
std::vector<int> perorderTravelsal(TreeNode* root) {
std::vector<int> res;
traversal(root, res);
return res;
}
};
1. leetcode 144 preorder traversal of binary tree
/**
* 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 traversal(TreeNode* root, vector<int>& res) {
if (root == NULL) {
return;
}
res.push_back(root->val);
traversal(root->left, res);
traversal(root->right, res);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
traversal(root, res);
return res;
}
};
//迭代
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> st;
if (root == nullptr) {
return res;
}
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top();
st.pop();
res.push_back(node->val);
if (node->right) {
st.push(node->right);
}
if (node->left) {
st.push(node->left);
}
}
return res;
}
};
//迭代 统一
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
if (root != NULL) st.push(root);
while (!st.empty()) {
TreeNode* node = st.top();
if (node != NULL) {
st.pop();
if (node->right) st.push(node->right); // 右
if (node->left) st.push(node->left); // 左
st.push(node); // 中
st.push(NULL);
} else {
st.pop();
node = st.top();
st.pop();
result.push_back(node->val);
}
}
return result;
}
};
2. Leetcode 145 post-order traversal of binary tree
//递归
class Solution {
public:
void traversal(TreeNode* root, vector<int>& res) {
if (root == nullptr) {
return;
}
traversal(root->left, res);
traversal(root->right, res);
res.push_back(root->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
traversal(root, res);
return res;
}
};
//迭代
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> st;
if (root == nullptr) {
return res;
}
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top();
st.pop();
res.push_back(node->val);
if (node->left) {
st.push(node->left);
}
if (node->right) {
st.push(node->right);
}
}
reverse(res.begin(), res.end());
return res;
}
};
//迭代 统一
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
if (root != NULL) st.push(root);
while (!st.empty()) {
TreeNode* node = st.top();
if (node != NULL) {
st.pop();
st.push(node); // 中
st.push(NULL);
if (node->right) st.push(node->right); // 右
if (node->left) st.push(node->left); // 左
} else {
st.pop();
node = st.top();
st.pop();
result.push_back(node->val);
}
}
return result;
}
};
3. Inorder traversal of leetcode 94 binary tree
//递归
/**
* 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 traversal(TreeNode* root, vector<int>& res) {
if (root == nullptr) {
return;
}
traversal(root->left, res);
res.push_back(root->val);
traversal(root->right, res);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
traversal(root, res);
return res;
}
};
//迭代
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur != nullptr || !st.empty()) {
if (cur != nullptr) {
//指针访问节点,访问到最底层
st.push(cur); //将访问的节点放入栈
cur = cur->left; //左
} else {
cur = st.top(); // 弹出的数据就是要处理的数据
st.pop();
res.push_back(cur->val); //中
cur = cur->right; //右
}
}
return res;
}
};
// 迭代 统一
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> st;
if (root != nullptr) {
st.push(root);
}
while (!st.empty()) {
TreeNode* node = st.top();
if (node != nullptr) {
st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
if (node->right) {
//添加右节点
st.push(node->right);
}
st.push(node); //添加中节点
st.push(nullptr); //中节点访问过,但是还没处理,加入空节点作为标记
if (node->left) {
st.push(node->left); //添加左节点
}
} else {
//只遇到空节点的时候,才将下一个节点放入res
st.pop(); //将空节点弹出
node = st.top(); // 重新取出栈中元素
st.pop();
res.push_back(node->val); //加入res
}
}
return res;
}
};
4. Level order traversal of leetcode 102 binary tree
/**
* 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<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
queue<TreeNode*> que;
if (root != nullptr) {
que.push(root);
}
while (!que.empty()) {
int size = que.size();
vector<int> vec;
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if (node->left) {
que.push(node->left);
}
if (node->right) {
que.push(node->right);
}
}
res.push_back(vec);
}
return res;
}
};