层序遍历
思路:结合了昨天学到的标记法,每当一层遍历完,向队列中添加一个NULL标记。遍历到NULL节点表示该层遍历完了,将结果存入结果集中。
/**
* 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*> q;
if (root == NULL)
return res;
q.push(root);
q.push(NULL);
TreeNode* t;
vector<int> tmp;
while (!q.empty()) {
if (q.front() == NULL) {
q.pop();
if (q.empty()) {
res.push_back(tmp);
break;
}
q.push(NULL);
res.push_back(tmp);
tmp.clear();
}
t = q.front();
tmp.push_back(t->val);
if (t->left)
q.push(t->left);
if (t->right)
q.push(t->right);
q.pop();
}
return res;
}
};
然而这个方法似乎并不够简洁。
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 = 1; i <= currentLevelSize; ++i) {
auto 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;
}
};
作者:力扣官方题解
链接:https://leetcode.cn/problems/binary-tree-level-order-traversal/solutions/241885/er-cha-shu-de-ceng-xu-bian-li-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目链接:107. 二叉树的层序遍历 II - 力扣(LeetCode)
本题要求反向输出层序遍历,代码不需要变动,别忘了vector可以颠倒。
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
auto levelOrder = vector<vector<int>>();
if (!root) {
return levelOrder;
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
auto level = vector<int>();
int size = q.size();
for (int i = 0; i < size; ++i) {
auto node = q.front();
q.pop();
level.push_back(node->val);
if (node->left) {
q.push(node->left);
}
if (node->right) {
q.push(node->right);
}
}
levelOrder.push_back(level);
}
reverse(levelOrder.begin(), levelOrder.end());
return levelOrder;
}
};
作者:力扣官方题解
链接:https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/solutions/402560/er-cha-shu-de-ceng-ci-bian-li-ii-by-leetcode-solut/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目链接:199. 二叉树的右视图 - 力扣(LeetCode)
层序遍历的变体,只需要保存每一层的最后一个就行。
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector <int> ret;
if (!root) {
return ret;
}
queue <TreeNode*> q;
q.push(root);
while (!q.empty()) {
int currentLevelSize = q.size();
for (int i = 1; i <= currentLevelSize; ++i) {
auto node = q.front(); q.pop();
if(i==currentLevelSize)
ret.push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return ret;
}
翻转二叉树
题目链接:226. 翻转二叉树 - 力扣(LeetCode)
思路:题目不难,还是层序遍历的思想,只要把每个进入队列的节点都转换一下就行了,每个节点只会入队一次,因此也只会转换一次。
class Solution {
public:
void swap(TreeNode *node){
if(node->left){
TreeNode* tmp=node->left;
node->left=node->right;
node->right=tmp;
}
else{
TreeNode* tmp=node->right;
node->right=node->left;
node->left=tmp;
}
}
TreeNode* invertTree(TreeNode* root) {
TreeNode* node;
queue<TreeNode*>q;
if(root==NULL)return root;
q.push(root);
while(!q.empty()){
node=q.front();
swap(node);
if(node->left)q.push(node->left);
if(node->right)q.push(node->right);
q.pop();
}
return root;
}
};
显然递归法更简洁,没想到竟然能递归自己。
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr) {
return nullptr;
}
TreeNode* left = invertTree(root->left);
TreeNode* right = invertTree(root->right);
root->left = right;
root->right = left;
return root;
}
};
作者:力扣官方题解
链接:https://leetcode.cn/problems/invert-binary-tree/solutions/415160/fan-zhuan-er-cha-shu-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
对称二叉树
思路:简单看了下讲解,自己手写出来了,虽然不是很简洁,但是逻辑清晰。
class Solution {
public:
bool compare(TreeNode* left,TreeNode*right){
if(left==NULL&&right==NULL)return true;
else if(left!=NULL&&right==NULL)return false;
else if(left==NULL&&right!=NULL)return false;
else if(left->val!=right->val)return false;
else {
return compare(left->left,right->right)&&compare(left->right,right->left);
}
}
bool isSymmetric(TreeNode* root) {
if(root==NULL)return true;
return compare(root->left,root->right);
}
};
这里留一个更简洁的写法,思路一致
class Solution {
public:
bool check(TreeNode *p, TreeNode *q) {
if (!p && !q) return true;
if (!p || !q) return false;
return p->val == q->val && check(p->left, q->right) && check(p->right, q->left);
}
bool isSymmetric(TreeNode* root) {
return check(root, root);
}
};
作者:力扣官方题解
链接:https://leetcode.cn/problems/symmetric-tree/solutions/268109/dui-cheng-er-cha-shu-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
标签:node,right,TreeNode,root,随想录,二叉树,return,第十五天,left From: https://www.cnblogs.com/Liubox/p/18011236