10.1 226. 翻转二叉树

  1. //遍历:前序
  2. class Solution {
  3. public:
  4. TreeNode* invertTree(TreeNode* root) {
  5. traverse(root);
  6. return root;
  7. }
  8. void traverse(TreeNode* root){
  9. if(root==NULL){
  10. return;
  11. }
  12. //交换左右子树
  13. TreeNode* temp=root->left;
  14. root->left=root->right;
  15. root->right=temp;
  16. traverse(root->left);
  17. traverse(root->right);
  18. }
  19. };
  20. //前序,迭代
  21. class Solution {
  22. public:
  23. TreeNode* invertTree(TreeNode* root) {
  24. if(root==NULL){
  25. return NULL;
  26. }
  27. stack<TreeNode*> st;
  28. st.push(root);
  29. while(!st.empty()){
  30. TreeNode* node=st.top();
  31. st.pop();
  32. //交换
  33. TreeNode* temp=node->left;
  34. node->left=node->right;
  35. node->right=temp;
  36. if(node->left){
  37. st.push(node->left);
  38. }
  39. if(node->right){
  40. st.push(node->right);
  41. }
  42. }
  43. return root;
  44. }
  45. };
  46. //层序
  47. class Solution {
  48. public:
  49. TreeNode* invertTree(TreeNode* root) {
  50. if(root==NULL){
  51. return NULL;
  52. }
  53. queue<TreeNode*> que;
  54. que.push(root);
  55. while(!que.empty()){
  56. int size=que.size();
  57. for(int i=0;i<size;i++){
  58. TreeNode* node=que.front();
  59. que.pop();
  60. //交换
  61. TreeNode* temp=node->left;
  62. node->left=node->right;
  63. node->right=temp;
  64. if(node->left){
  65. que.push(node->left);
  66. }
  67. if(node->right){
  68. que.push(node->right);
  69. }
  70. }
  71. }
  72. return root;
  73. }
  74. };
  1. //分解->递归
  2. class Solution {
  3. public:
  4. TreeNode* invertTree(TreeNode* root) {
  5. if(root==NULL){
  6. return NULL;
  7. }
  8. TreeNode *left=invertTree(root->left);
  9. TreeNode *right=invertTree(root->right);
  10. root->left=right;
  11. root->right=left;
  12. return root;
  13. }
  14. };

10.2 116. 填充每个节点的下一个右侧节点指针

  1. //遍历
  2. class Solution {
  3. public:
  4. Node* connect(Node* root) {
  5. if(root==NULL){
  6. return NULL;
  7. }
  8. traverse(root->left,root->right);
  9. return root;
  10. }
  11. void traverse(Node* node1,Node* node2){
  12. if(node1==NULL||node2==NULL){
  13. return ;
  14. }
  15. node1->next=node2;
  16. traverse(node1->left,node1->right);
  17. traverse(node2->left,node2->right);
  18. traverse(node1->right,node2->left);
  19. }
  20. };
  21. //不能分解
  22. //层序遍历
  23. class Solution {
  24. public:
  25. Node* connect(Node* root) {
  26. queue<Node*> que;
  27. if(root!=NULL){
  28. que.push(root);
  29. }
  30. while(!que.empty()){
  31. int size=que.size();
  32. //记录上一层最后一个,连接到这一层的头
  33. Node* nodePre;
  34. Node* node;
  35. for(int i=0;i<size;i++){
  36. if(i==0){
  37. nodePre=que.front();
  38. que.pop();
  39. node=nodePre;
  40. }else{
  41. node=que.front();
  42. que.pop();
  43. nodePre->next=node;
  44. nodePre=nodePre->next;
  45. }
  46. if(node->left){
  47. que.push(node->left);
  48. }
  49. if(node->right){
  50. que.push(node->right);
  51. }
  52. }
  53. nodePre->next=NULL;
  54. }
  55. return root;
  56. }
  57. };

10.3 114. 二叉树展开为链表

  1. //分解
  2. class Solution {
  3. public:
  4. void flatten(TreeNode* root) {
  5. if(root==NULL){
  6. return;
  7. }
  8. flatten(root->left);
  9. flatten(root->right);
  10. TreeNode* left=root->left;
  11. TreeNode* right=root->right;
  12. root->left=NULL;
  13. root->right=left;
  14. //找到当前树的最右边,连上right
  15. TreeNode* p=root;
  16. while(p->right){
  17. p=p->right;
  18. }
  19. p->right=right;
  20. }
  21. };

10.4 654. 最大二叉树

  1. class Solution {
  2. public:
  3. TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
  4. return build(nums,0,nums.size()-1);
  5. }
  6. TreeNode* build(vector<int>& nums,int left,int right){
  7. if(right<left){
  8. return NULL;
  9. }
  10. int m_max=left; //最大值的索引
  11. for(int i=left+1;i<=right;i++){
  12. if(nums[m_max]<nums[i]){
  13. m_max=i;
  14. }
  15. }
  16. //找到最大值后当作根节点
  17. TreeNode* root=new TreeNode(nums[m_max]);
  18. //构造左子树
  19. root->left=build(nums,left,m_max-1);
  20. //构造右子树
  21. root->right=build(nums,m_max+1,right);
  22. return root;
  23. }
  24. };

10.5 105. 从前序与中序遍历序列构造二叉树

  1. class Solution {
  2. public:
  3. TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
  4. return build(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1);
  5. }
  6. TreeNode* build(vector<int>& preorder,int preStart,int preEnd, vector<int>& inorder,int inStart,int inEnd){
  7. if(preStart>preEnd){
  8. return NULL;
  9. }
  10. //前序第一个元素为根
  11. int rootVal=preorder[preStart];
  12. //在中序中找到根的index
  13. int index=0;
  14. for(int i=inStart;i<=inEnd;i++){
  15. if(inorder[i]==rootVal){
  16. index=i;
  17. break;
  18. }
  19. }
  20. int len=index-inStart;
  21. TreeNode* root=new TreeNode(rootVal);
  22. root->left=build(preorder,preStart+1,preStart+len,inorder,inStart,index-1);
  23. root->right=build(preorder,preStart+len+1,preEnd,inorder,index+1,inEnd);
  24. return root;
  25. }
  26. };

10.6 106. 从中序与后序遍历序列构造二叉树

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;
    }
};

10.7 889. 根据前序和后序遍历构造二叉树

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;
    }
};

10.8 543. 二叉树的直径

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);
    }
};

10.9 230. 二叉搜索树中第K小的元素

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);
    }
};

10.10 538. 把二叉搜索树转换为累加树

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);
    }
};

10.11 1038. 从二叉搜索树到更大和树

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;
    }
};

10.12 144. 二叉树的前序遍历

//递归
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;
    }
};

10.13 94. 二叉树的中序遍历

//递归
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;
    }
};

10.14 145. 二叉树的后序遍历

//递归
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;
    }
};

10.15 102. 二叉树的层序遍历

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;
    }
};

10.16 107. 二叉树的层序遍历 II

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;
    }
};

10.17 199. 二叉树的右视图

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;
    }
};

10.18 637. 二叉树的层平均值

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;
    }
};

10.19 429. N 叉树的层序遍历

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;
    }
};

10.20 515. 在每个树行中找最大值

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;
    }
};

10.21 117. 填充每个节点的下一个右侧节点指针 II116. 填充每个节点的下一个右侧节点指针

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;
    }
};

10.22 104. 二叉树的最大深度

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;
    }
};

10.23 111. 二叉树的最小深度

//层序遍历
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;
    }
};

10.24 95. 不同的二叉搜索树 II

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;
    }
};