二叉树
二叉树的遍历
满二叉树 只有度为0或者度为2的节点,并且度为0的节点在同一层上; 满二叉树的深度为k, 节点数为 2^k - 1; 完全二叉树 除了最底层节点没填满外,其余没层节点数都达到了最大值,并且最下面一层的节点都集中在该层最左边的位置; 优先级队列其实就是一个堆,堆就是一颗完全二叉树,同时保证父子节点的顺序关系。 二叉搜索树 平衡二叉搜索树(AVL): 它是一颗空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两颗子树都是一颗平衡二叉树。 C++中map、set、multimap、multiset的底层实现都是平衡二叉树,所以map、set的增删操作时间的复杂度logn 但是unordered_map、unordered_set的底层实现是哈希表 二叉树的存储方式,链式存储、顺序存储 数组存储二叉树,父节点的下标是i,左孩子i2 + 1 右孩子i2 + 2; 深度优先遍历(前、中、后序遍历) 广度优先遍历(层序遍历)
#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 二叉树的前序遍历
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 二叉树的后序遍历
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. leetcode 94 二叉树的中序遍历
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 {
st.pop();
node = st.top();
st.pop();
res.push_back(node->val);
}
}
return res;
}
};
4. leetcode 102 二叉树的层序遍历
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;
}
};
|