//遍历:前序
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
traverse(root);
return root;
}
void traverse(TreeNode* root){
if(root==NULL){
return;
}
//交换左右子树
TreeNode* temp=root->left;
root->left=root->right;
root->right=temp;
traverse(root->left);
traverse(root->right);
}
};
//前序,迭代
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==NULL){
return NULL;
}
stack<TreeNode*> st;
st.push(root);
while(!st.empty()){
TreeNode* node=st.top();
st.pop();
//交换
TreeNode* temp=node->left;
node->left=node->right;
node->right=temp;
if(node->left){
st.push(node->left);
}
if(node->right){
st.push(node->right);
}
}
return root;
}
};
//层序
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==NULL){
return NULL;
}
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
int size=que.size();
for(int i=0;i<size;i++){
TreeNode* node=que.front();
que.pop();
//交换
TreeNode* temp=node->left;
node->left=node->right;
node->right=temp;
if(node->left){
que.push(node->left);
}
if(node->right){
que.push(node->right);
}
}
}
return root;
}
};
//分解->递归
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==NULL){
return NULL;
}
TreeNode *left=invertTree(root->left);
TreeNode *right=invertTree(root->right);
root->left=right;
root->right=left;
return root;
}
};
//遍历
class Solution {
public:
Node* connect(Node* root) {
if(root==NULL){
return NULL;
}
traverse(root->left,root->right);
return root;
}
void traverse(Node* node1,Node* node2){
if(node1==NULL||node2==NULL){
return ;
}
node1->next=node2;
traverse(node1->left,node1->right);
traverse(node2->left,node2->right);
traverse(node1->right,node2->left);
}
};
//不能分解
//层序遍历
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if(root!=NULL){
que.push(root);
}
while(!que.empty()){
int size=que.size();
//记录上一层最后一个,连接到这一层的头
Node* nodePre;
Node* node;
for(int i=0;i<size;i++){
if(i==0){
nodePre=que.front();
que.pop();
node=nodePre;
}else{
node=que.front();
que.pop();
nodePre->next=node;
nodePre=nodePre->next;
}
if(node->left){
que.push(node->left);
}
if(node->right){
que.push(node->right);
}
}
nodePre->next=NULL;
}
return root;
}
};
//分解
class Solution {
public:
void flatten(TreeNode* root) {
if(root==NULL){
return;
}
flatten(root->left);
flatten(root->right);
TreeNode* left=root->left;
TreeNode* right=root->right;
root->left=NULL;
root->right=left;
//找到当前树的最右边,连上right
TreeNode* p=root;
while(p->right){
p=p->right;
}
p->right=right;
}
};
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return build(nums,0,nums.size()-1);
}
TreeNode* build(vector<int>& nums,int left,int right){
if(right<left){
return NULL;
}
int m_max=left; //最大值的索引
for(int i=left+1;i<=right;i++){
if(nums[m_max]<nums[i]){
m_max=i;
}
}
//找到最大值后当作根节点
TreeNode* root=new TreeNode(nums[m_max]);
//构造左子树
root->left=build(nums,left,m_max-1);
//构造右子树
root->right=build(nums,m_max+1,right);
return root;
}
};
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
return build(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1);
}
TreeNode* build(vector<int>& preorder,int preStart,int preEnd, vector<int>& inorder,int inStart,int inEnd){
if(preStart>preEnd){
return NULL;
}
//前序第一个元素为根
int rootVal=preorder[preStart];
//在中序中找到根的index
int index=0;
for(int i=inStart;i<=inEnd;i++){
if(inorder[i]==rootVal){
index=i;
break;
}
}
int len=index-inStart;
TreeNode* root=new TreeNode(rootVal);
root->left=build(preorder,preStart+1,preStart+len,inorder,inStart,index-1);
root->right=build(preorder,preStart+len+1,preEnd,inorder,index+1,inEnd);
return root;
}
};
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return build(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1);
}
TreeNode* build(vector<int>& inorder, int inStart,int inEnd,vector<int>& postorder,int postStart,int postEnd) {
if(inStart>inEnd){
return NULL;
}
int rootVal=postorder[postEnd];
int index=0;
for(int i=inStart;i<=inEnd;i++){
if(inorder[i]==rootVal){
index=i;
break;
}
}
int len=index-inStart;
TreeNode* root=new TreeNode(rootVal);
root->left=build(inorder,inStart,index-1,postorder,postStart,postStart+len-1);
root->right=build(inorder,index+1,inEnd,postorder,postStart+len,postEnd-1);
return root;
}
};
class Solution {
public:
TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {
return build(preorder,0,preorder.size()-1,postorder,0,postorder.size()-1);
}
TreeNode* build(vector<int>& preorder, int preStart,int preEnd,vector<int>& postorder,int postStart,int postEnd){
if(preStart>preEnd){
return NULL;
}
if(preStart==preEnd){
return new TreeNode(preorder[preStart]);
}
int rootVal=preorder[preStart];
int leftrootVal=preorder[preStart+1];
int index=0;
for(int i=postStart;i<=postEnd;i++){
if(postorder[i]==leftrootVal){
index=i;
break;
}
}
int len=index-postStart+1;
TreeNode* root=new TreeNode(rootVal);
root->left=build(preorder,preStart+1,preStart+len,postorder,postStart,index);
root->right=build(preorder,preStart+len+1,preEnd,postorder,index+1,postEnd-1);
return root;
}
};
class Solution {
public:
int m_max=0;
int diameterOfBinaryTree(TreeNode* root) {
maxDepth(root);
return m_max;
}
int maxDepth(TreeNode* root){
if(root==NULL){
return 0;
}
int leftMax=maxDepth(root->left);
int rightMax=maxDepth(root->right);
//直径为相加
int diameter=leftMax+rightMax;
m_max=max(m_max,diameter);
return 1+max(leftMax,rightMax);
}
};
class Solution {
public:
int rank=0; //记录排名,第几小的元素
int res=0; //记录结果,这个元素是谁
int kthSmallest(TreeNode* root, int k) {
traverse(root,k);
return res;
}
void traverse(TreeNode* root,int k){
if(root==NULL){
return;
}
traverse(root->left,k);
rank++;
if(k==rank){
res=root->val;
return;
}
traverse(root->right,k);
}
};
class Solution {
public:
TreeNode* convertBST(TreeNode* root) {
traverse(root);
return root;
}
int sum=0;
void traverse(TreeNode* root){
if(root==NULL){
return ;
}
traverse(root->right);
sum+=root->val;
root->val=sum;
traverse(root->left);
}
};
class Solution {
public:
int sum=0;
TreeNode* bstToGst(TreeNode* root) {
return traverse(root);
}
TreeNode* traverse(TreeNode* root){
if(root==NULL){
return NULL;
}
traverse(root->right);
sum+=root->val;
root->val=sum;
traverse(root->left);
return root;
}
};
//递归
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
traverse(root,result);
return result;
}
void traverse(TreeNode* root,vector<int>& result){
if(root==NULL){
return;
}
result.push_back(root->val);
traverse(root->left,result);
traverse(root->right,result);
}
};
//迭代
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> result;
if(root==NULL){
return result;
}
st.push(root);
while(!st.empty()){
TreeNode* node=st.top();
st.pop();
result.push_back(node->val);
if(node->right){
st.push(node->right);
}
if(node->left){
st.push(node->left);
}
}
return result;
}
};
//递归
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
traverse(root,result);
return result;
}
void traverse(TreeNode* root,vector<int>& result){
if(root==NULL){
return;
}
traverse(root->left,result);
result.push_back(root->val);
traverse(root->right,result);
}
};
//迭代
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
while(root!=NULL||!st.empty()){
if(root!=NULL){
//先把root push进栈,等待后续访问
st.push(root);
root=root->left;
}else{
//开始访问,顺便把右子push进去
TreeNode* node=st.top();e
result.push_back(node->val);
root=node->right;
}
}
return result;
}
};
//递归
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
traverse(root,result);
return result;
}
void traverse(TreeNode* root,vector<int>& result){
if(root==NULL){
return;
}
traverse(root->left,result);
traverse(root->right,result);
result.push_back(root->val);
}
};
//迭代
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> result;
if(root==NULL){
return result;
}
st.push(root);
while(!st.empty()){
TreeNode* node=st.top();
st.pop();
result.push_back(node->val);
if(node->left){
st.push(node->left);
}
if(node->right){
st.push(node->right);
}
}
reverse(result.begin(),result.end());
return result;
}
};
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if(root!=NULL) que.push(root);
vector<vector<int>> result;
while(!que.empty()){
int size=que.size();
vector<int> vec;
for(int i=0;i<size;i++){
TreeNode* node=que.front();
que.pop();
vec.push_back(node->val);
if(node->left){
que.push(node->left);
}
if(node->right){
que.push(node->right);
}
}
result.push_back(vec);
}
return result;
}
};
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
queue<TreeNode*> que;
if(root!=NULL){
que.push(root);
}
vector<vector<int>> result;
while(!que.empty()){
int size=que.size();
vector<int> vec;
for(int i=0;i<size;i++){
TreeNode* node=que.front();
que.pop();
vec.push_back(node->val);
if(node->left!=NULL){
que.push(node->left);
}
if(node->right!=NULL){
que.push(node->right);
}
}
result.push_back(vec);
}
reverse(result.begin(),result.end()); //区别在此
return result;
}
};
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
queue<TreeNode*> que;
if(root!=NULL){
que.push(root);
}
vector<int> result;
while(!que.empty()){
int size=que.size();
for(int i=0;i<size;i++){
TreeNode* node=que.front();
que.pop();
if(i==size-1){
result.push_back(node->val);
}
if(node->left){
que.push(node->left);
}
if(node->right){
que.push(node->right);
}
}
}
return result;
}
};
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
queue<TreeNode*> que;
if(root!=NULL){
que.push(root);
}
vector<double> result;
while(!que.empty()){
int size=que.size();
double sum=0;
for(int i=0;i<size;i++){
TreeNode* node=que.front();
que.pop();
sum+=node->val;
if(node->left!=NULL){
que.push(node->left);
}
if(node->right!=NULL){
que.push(node->right);
}
}
result.push_back(sum/size);
}
return result;
}
};
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
queue<Node*> que;
if(root!=NULL) que.push(root);
vector<vector<int>> result;
while(!que.empty()){
int size=que.size();
vector<int> vec;
for(int i=0;i<size;i++){
Node* node=que.front();
que.pop();
vec.push_back(node->val);
for(int i=0;i<node->children.size();i++){
if(node->children[i]){
que.push(node->children[i]);
}
}
}
result.push_back(vec);
}
return result;
}
};
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
queue<TreeNode*> que;
if(root!=NULL){
que.push(root);
}
vector<int> result;
while(!que.empty()){
int size=que.size();
int m_max=INT_MIN;
for(int i=0;i<size;i++){
TreeNode* node=que.front();
que.pop();
m_max=max(m_max,node->val);
if(node->left){
que.push(node->left);
}
if(node->right){
que.push(node->right);
}
}
result.push_back(m_max);
}
return result;
}
};
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if(root!=NULL){
que.push(root);
}
while(!que.empty()){
int size=que.size();
//记录上一层最后一个,连接到这一层的头
Node* nodePre;
Node* node;
for(int i=0;i<size;i++){
if(i==0){
nodePre=que.front();
que.pop();
node=nodePre;
}else{
node=que.front();
que.pop();
nodePre->next=node;
nodePre=nodePre->next;
}
if(node->left){
que.push(node->left);
}
if(node->right){
que.push(node->right);
}
}
nodePre->next=NULL;
}
return root;
}
};
class Solution {
public:
//1.函数意义:找到二叉树的最大深度
int maxDepth(TreeNode* root) {
queue<TreeNode*> que;
if(root!=NULL){
que.push(root);
}
int depth=0;
while(!que.empty()){
int size=que.size();
depth++;
for(int i=0;i<size;i++){
TreeNode* node=que.front();
que.pop();
if(node->left){
que.push(node->left);
}
if(node->right){
que.push(node->right);
}
}
}
return depth;
}
};
//层序遍历
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> que;
if(root!=NULL){
que.push(root);
}
int depth=0;
while(!que.empty()){
int size=que.size();
depth++;
for(int i=0;i<size;i++){
TreeNode* node=que.front();
que.pop();
if(node->left){
que.push(node->left);
}
if(node->right){
que.push(node->right);
}
//左右子树都空,才是最低一层
if(!node->left&& !node->right){
return depth;
}
}
}
return depth;
}
};
//递归
class Solution {
public:
int minDepth(TreeNode* root) {
if(root==NULL){
return 0;
}
if(root->left==NULL&&root->right==NULL){
return 1;
}
int m_min=INT_MAX;
if(root->left){
m_min=min(m_min,minDepth(root->left));
}
if(root->right){
m_min=min(m_min,minDepth(root->right));
}
return m_min+1;
}
};
class Solution {
public:
vector<TreeNode*> generateTrees(int n) {
return build(1,n);
}
vector<TreeNode*> build(int low,int high){
vector<TreeNode*> res;
if(low>high){
res.push_back(NULL);
return res;
}
for(int i=low;i<=high;i++){
vector<TreeNode*> left=build(low,i-1);
vector<TreeNode*> right=build(i+1,high);
for(TreeNode *node1:left){
for(TreeNode *node2:right){
TreeNode* root=new TreeNode(i);
root->left=node1;
root->right=node2;
res.push_back(root);
}
}
}
return res;
}
};