102. 二叉树的层序遍历
class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>>result; queue<TreeNode*>qe; if(root!=nullptr)qe.push(root); while(!qe.empty()){ int size=qe.size(); vector<int>temp; for(int i=0;i<size;i++){ temp.push_back(qe.front()->val); if(qe.front()->left)qe.push(qe.front()->left); if(qe.front()->right)qe.push(qe.front()->right); qe.pop(); } result.push_back(temp); } return result; } };
107. 二叉树的层序遍历 II
class Solution { public: vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>>result; queue<TreeNode*>qe; if(root!=NULL)qe.push(root); while(!qe.empty()){ int size=qe.size(); vector<int>temp; for(int i=0;i<size;i++){ temp.push_back(qe.front()->val); if(qe.front()->left)qe.push(qe.front()->left); if(qe.front()->right)qe.push(qe.front()->right); qe.pop(); } result.push_back(temp); } reverse(result.begin(),result.end()); return result; } };
199. 二叉树的右视图
class Solution { public: vector<int> rightSideView(TreeNode* root) { vector<int>result; queue<TreeNode* >qe; if(root!=nullptr)qe.push(root),result.push_back(qe.front()->val); while(!qe.empty()){ int size=qe.size(); for(int i=0;i<size;i++){ if(qe.front()->right)qe.push(qe.front()->right); if(qe.front()->left) qe.push(qe.front()->left); qe.pop(); } if(!qe.empty())result.push_back(qe.front()->val); } return result; } };
226. 翻转二叉树
层序遍历做法
class Solution { public: TreeNode* invertTree(TreeNode* root) { queue<TreeNode* >qe; if(root!=nullptr)qe.push(root); while(!qe.empty()){ int size=qe.size(); for(int i=0;i<size;i++){ if(qe.front()->right)qe.push(qe.front()->right); if(qe.front()->left) qe.push(qe.front()->left); TreeNode* temp=qe.front()->left; qe.front()->left=qe.front()->right; qe.front()->right=temp; qe.pop(); } } return root; } };
前序遍历(递归)做法
class Solution { public: TreeNode* invertTree(TreeNode* root) { if(root==nullptr)return root; TreeNode* temp=root->left; root->left=root->right; root->right=temp; invertTree(root->left); invertTree(root->right); return root; } };
101. 对称二叉树
class Solution { public: bool isSymmetric(TreeNode* root) { queue<TreeNode* >qe; if(root!=nullptr)qe.push(root); while(!qe.empty()){ vector<int>result; int size=qe.size(); for(int i=0;i<size;i++){ if(qe.front()->left) qe.push(qe.front()->left),result.push_back(qe.front()->left->val); else result.push_back(101); if(qe.front()->right)qe.push(qe.front()->right),result.push_back(qe.front()->right->val); else result.push_back(101); qe.pop(); } vector<int>result1(result); reverse(result.begin(),result.end()); if(result1!=result)return 0; } return 1; } };
后序遍历做法(递归)。
从根的左右子树进入,分为左右中,右左中两只遍历方式,分外侧和内侧比较
class Solution { public: bool compare(TreeNode* left,TreeNode* right){ if(left==NULL&&right==NULL)return 1; else if(left==NULL&&right!=NULL)return 0; else if(left!=NULL&&right==NULL)return 0; else if(left->val!=right->val)return 0; bool outside= compare(left->left,right->right); bool isside= compare(left->right,right->left); bool isSame= outside&&isside; return isSame; } bool isSymmetric(TreeNode* root) { if(root==NULL)return 1; return compare(root->left,root->right); } };
标签:right,层序,随想录,push,qe,二叉树,front,root,left From: https://www.cnblogs.com/zhishikele/p/17044196.html