二叉树相关
二叉树的数组形式:
前序遍历:[ 根节点, [左子树的前序遍历结果], [右子树的前序遍历结果] ]
中序遍历:[ [左子树的中序遍历结果], 根节点, [右子树的中序遍历结果] ]
后序遍历:[ [左子树的中序遍历结果], [右子树的中序遍历结果],根节点]
层序遍历
广度优先遍历
代码:
对于每层的节点,放到一个用来处理节点队列里,另外每个队列对应一个vector,将每层节点放进vector
对于每个队列,弹出先放的元素q.front,将这个元素放进vector,
随后,将左节点和右子节点放进队列
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector <vector <int>> ret;
if (!root) {
return ret;
}
queue <TreeNode*> q;
q.push(root);
while (!q.empty()) {
int currentLevelSize = q.size();
ret.push_back(vector <int> ());
for (int i = 0; i < currentLevelSize; ++i) {
TreeNode* node = q.front();
q.pop();
ret.back().push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return ret;
}
};
对称问题
226 翻转二叉树
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root == nullptr){
return root;
}
swap(root->left,root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
};
迭代法,使用栈进行前序遍历
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == NULL) return root;
stack<TreeNode*> st;
st.push(root);
while(!st.empty()) {
TreeNode* node = st.top(); // 中
st.pop();
swap(node->left, node->right);
if(node->right) st.push(node->right); // 右
if(node->left) st.push(node->left); // 左
}
return root;
}
};
101 对称二叉树
//重点在于递归的时候左右节点的选择
class Solution {
public:
bool compare(TreeNode* left , TreeNode* right){
if(left==nullptr && right==nullptr){
return true;
}else if(left == nullptr && right!=nullptr)
{
return false;
}
else if(left!=nullptr && right==nullptr){
return false;
}
else if(left->val != right->val){
return false;
}
//开始递归子树
//判断内侧
bool insideSame = compare(left->right,right->left);
//判断外侧
bool outsideSame = compare(left->left,right->right);
bool same = insideSame && outsideSame;
return same;
}
bool isSymmetric(TreeNode* root) {
if(root==nullptr){
return true;
}
return compare(root->left,root->right);
}
};
完全二叉树
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。
若最底层为第 h 层,则该层包含 1~ 2h 个节点。满二叉树是一种特殊的二叉树.
普通的二叉树个数计算:
class Solution {
public:
int countNodes(TreeNode* root) {
int num = 0;
if(root==nullptr){
return num;
}
int leftNum = countNodes(root->left);
int rightNum = countNodes(root->right);
num = leftNum+rightNum + 1;
return num;
}
};
- 完全二叉树个数
这道题可以用满二叉树的性质优化计算
class Solution {
public:
int countNodes(TreeNode* root) {
int num = 0;
if(root == nullptr){
return 0;
}
TreeNode* left = root->left;
TreeNode* right = root->right;
int leftH = 0,rightH =0;
while(left){
left=left->left;
leftH ++;
}
while(right){
right = right->right;
rightH ++;
}
//如果左右子树高度相同,则是满二叉树
if(leftH == rightH){
return (2<<leftH) - 1;
}
return countNodes(root->left)+countNodes(root->right)+1;
}
};
## 层序遍历
主要难点在于每层要用容器遍历,遍历完后弹当前节点,检索下一层入队
class Solution {
public:
vector<vector
vector <vector
if (!root) {
return ret;
}
//这个是每一层的队列
queue <TreeNode*> q;
q.push(root);
while (!q.empty()) {
int currentLevelSize = q.size();
ret.push_back(vector <int> ());
for (int i = 1; i <= currentLevelSize; ++i) {
//弹出首位,子节点进队列
TreeNode* node = q.front();
q.pop();
ret.back().push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return ret;
}
};