层次遍历题目
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
//排除特殊情况
vector<vector<int>> result;
if(root==nullptr) return result;
queue<TreeNode*> st;
st.push(root);
while(!st.empty()){
int size=st.size();
vector<int> vec;
for(int i=0;i<size;i++){
TreeNode* tmp=st.front();
vec.push_back(tmp->val);
st.pop();
if(tmp->left!=nullptr) st.push(tmp->left);
if(tmp->right!=nullptr) st.push(tmp->right);
}
result.push_back(vec);
}
return result;
}
};
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
//排除特殊情况
vector<vector<int>> result;
if(root==nullptr) return result;
queue<TreeNode*> st;
st.push(root);
while(!st.empty()){
int size=st.size();
vector<int> vec;
for(int i=0;i<size;i++){
TreeNode* tmp=st.front();
vec.push_back(tmp->val);
st.pop();
if(tmp->left!=nullptr) st.push(tmp->left);
if(tmp->right!=nullptr) st.push(tmp->right);
// reverse(vec.begin(),vec.end());
}
result.push_back(vec);
}
reverse(result.begin(),result.end());
return result;
}
};
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> result;
if(root==nullptr) return result;
queue<TreeNode*> st;
st.push(root);
while(!st.empty()){
int size=st.size();
TreeNode* tmp=st.front();
for(int i=0;i<size;i++){
tmp=st.front();
st.pop();
if(tmp->left!=nullptr) st.push(tmp->left);
if(tmp->right!=nullptr) st.push(tmp->right);
// reverse(vec.begin(),vec.end());
}
result.push_back(tmp->val);
}
return result;
}
};
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> result;
if(root==nullptr) return result;
queue<TreeNode*> st;
st.push(root);
while(!st.empty()){
int size=st.size();
vector<int> vec;
double ave=0;
for(int i=0;i<size;i++){
TreeNode* tmp=st.front();
ave=tmp->val+ave;
st.pop();
if(tmp->left!=nullptr) st.push(tmp->left);
if(tmp->right!=nullptr) st.push(tmp->right);
}
ave=ave/size;
result.push_back(ave);
}
return result;
}
};
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
queue<Node*> st;
if (root != NULL) st.push(root);
vector<vector<int>> result;
while(!st.empty()){
int size=st.size();
vector<int> vec;
for(int i=0;i<size;i++){
Node* tmp=st.front();
vec.push_back(tmp->val);
st.pop();
for (int i = 0; i < tmp->children.size(); i++) { // 将节点孩子加入队列
if (tmp->children[i]) st.push(tmp->children[i]);
}
}
result.push_back(vec);
}
return result;
}
};
101对称二叉树 递归版 1.确定返回值和参数 2.确定递归终止条件 3.设计单层递归逻辑
class Solution {
public:
bool compare(TreeNode* leftnode,TreeNode* rightnode){
//确定递归终止条件
// 递归终止条件(覆盖所有边界情况)
if (leftnode == nullptr && rightnode == nullptr) return true; // 都空,对称
if (leftnode == nullptr || rightnode == nullptr) return false; // 一个空一个非空,不对称
if (leftnode->val != rightnode->val) return false; // 值不相等,不对称
//确定单层递归逻辑
bool outside = compare(leftnode->left, rightnode->right); // 左子树:左、 右子树:右
bool inside = compare(leftnode->right, rightnode->left); // 左子树:右、 右子树:左
bool isSame = outside && inside; // 左子树:中、 右子树:中 (逻辑处理)
return isSame;
}
bool isSymmetric(TreeNode* root) {
//采用递归法进行验证
//初始验证
if (root == NULL) return true;
return compare(root->left, root->right);
}
};
101对称二叉树 迭代版
class Solution {
public:
bool isSymmetric(TreeNode* root) {
//采用队列进行判定
queue<TreeNode*> compare;
if(root==nullptr) return true;
//讲左右孩子入队
compare.push(root->left);
compare.push(root->right);
while(!compare.empty()){
TreeNode* left=compare.front();compare.pop();
TreeNode* right=compare.front();compare.pop();
//比较左右节点
if(left==nullptr&&right==nullptr)
continue;
// 左右一个节点不为空,或者都不为空但数值不相同,返回false
if ((!left || !right || (left->val != right->val))) {
return false;
}
//入队操作
compare.push(left->left);
compare.push(right->right);
compare.push(left->right);
compare.push(right->left);
}
return true;
}
};
最大深度计算 递归。考虑,递归是否是需要通过设计额外的外层函数调用来实现,也就是说,我们往往第一步的阻力是怎么设置传参和返回,然后才是终止条件和单层递归设计。
class Solution {
public:
int getdepth(TreeNode* node) {
if (node == NULL) return 0;
int leftdepth = getdepth(node->left); // 左
int rightdepth = getdepth(node->right); // 右
int depth = 1 + max(leftdepth, rightdepth); // 中
return depth;
}
int maxDepth(TreeNode* root) {
//采用递归方法进行计算
return getdepth(root);
}
};