102. 二叉树的层序遍历
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> myqueue;
int layer = 0;
if (root) myqueue.push(root);
vector<vector<int>> result;
while (!myqueue.empty()) {
int size = myqueue.size();
vector<int> tem_layer;
for (int i = 0; i < size; i++) {
TreeNode* tem = myqueue.front();
tem_layer.push_back(tem->val);
myqueue.pop();
if (tem->left) myqueue.push(tem->left);
if (tem->right) myqueue.push(tem->right);
}
result.push_back(tem_layer);
}
return result;
}
};
226. 翻转二叉树
参考
思路: 递归法
明确遍历顺序, 使用后序或前序遍历
//1.后序遍历
class Solution {
public:
TreeNode* invertTree(TreeNode* root) { //1. 明确递归函数的参数和返回值
if (root == nullptr) return root; //2. 确认终止条件
TreeNode* left = invertTree(root->left); //3. 单层递归逻辑
TreeNode* right = invertTree(root->right);
root->left = right;
root->right = left;
return root; //通常这里返回最终结果,在递归的第一层
}
};
//2.前序遍历
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == NULL) return root;
swap(root->left, root->right); // 中
invertTree(root->left); // 左
invertTree(root->right); // 右
return root;
}
};
思路: 迭代法实现
//前序迭代法
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
stack<TreeNode*> mystack;
if (root) mystack.push(root);
while (!mystack.empty()) {
TreeNode* tem = mystack.top();
mystack.pop();
swap(tem->right, tem->left);
if (tem->right) mystack.push(tem->right);
if (tem->left) mystack.push(tem->left);
}
return root;
}
};
//统一迭代法(前序)
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
stack<TreeNode*> mystack;
if(root) mystack.push(root);
while (!mystack.empty()) {
TreeNode* tem = mystack.top();
if (tem != nullptr) {
mystack.pop();
if (tem->right) mystack.push(tem->right);
if (tem->left) mystack.push(tem->left);
mystack.push(tem);
mystack.push(nullptr);
} else {
mystack.pop();
swap(mystack.top()->left,mystack.top()->right);
mystack.pop();
}
}
return root;
}
};
思路: 层序遍历实现
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
queue<TreeNode*> myqueue;
if (root) myqueue.push(root);
while (!myqueue.empty()) {
int size = myqueue.size();
for (int i = 0; i < size; i++) {
TreeNode* tem = myqueue.front();
myqueue.pop();
if (tem->left) myqueue.push(tem->left);
if (tem->right) myqueue.push(tem->right);
swap(tem->left, tem->right);
}
}
return root;
}
};
101. 对称二叉树
思路: 递归法
- 递归函数的参数和返回值
- 确定终止条件
- 确定单层递归逻辑
class Solution {
bool judge(TreeNode* left, TreeNode* right) {
if (left == nullptr && right == nullptr) return true;
else if (left == nullptr || right == nullptr) return false;
else if (left->val != right->val) return false;//left->val == right->val return true为错误操作
//else return false;//错误操作
bool res1 = judge(left->left, right->right);
bool res2 = judge(left->right, right->left);
return res1 && res2;
}
public:
bool isSymmetric(TreeNode* root) {
if (root == nullptr) return true;
return judge(root->left, root->right);
}
};
思路: 迭代法
//非层序遍历, 但其结构类似
相关题目
102.二叉树的层序遍历
107.二叉树的层次遍历 II
199.二叉树的右视图
637.二叉树的层平均值
429.N叉树的层序遍历
515.在每个树行中找最大值
116.填充每个节点的下一个右侧节点指针
117.填充每个节点的下一个右侧节点指针II
104.二叉树的最大深度
111.二叉树的最小深度