【leetcode算法练习】二叉树练习

https://labuladong.gitbook.io/algo/di-ling-zhang-bi-du-xi-lie/xue-xi-shu-ju-jie-gou-he-suan-fa-de-gao-xiao-fang-fa

目录

套框架刷通二叉树|第一期  (参考)

东哥手把手帮你刷通二叉树|第二期

226. Invert Binary Tree

116. Populating Next Right Pointers in Each Node

114. Flatten Binary Tree to Linked List

654. Maximum Binary Tree

 105. Construct Binary Tree from Preorder and Inorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal


 

套框架刷通二叉树|第一期  (参考)

https://blog.csdn.net/fdl123456/article/details/108613388

东哥手把手帮你刷通二叉树|第二期

https://blog.csdn.net/fdl123456/article/details/108744582

把题目的要求细化,搞清楚根节点应该做什么,然后剩下的事情抛给前/中/后序的遍历框架就行了,我们千万不要跳进递归的细节里!

问题解决

226. Invert Binary Tree

题目链接:https://leetcode.com/problems/invert-binary-tree/

AC代码:递归思想 前序遍历

TreeNode* invertTree(TreeNode* root) {
       if(root==NULL)  return root;
        
        //左右反转
        TreeNode* tmp=root->left;
        root->left=root->right;
        root->right=tmp;
        //递归
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }

116. Populating Next Right Pointers in Each Node

题目链接:https://leetcode.com/problems/populating-next-right-pointers-in-each-node/

//增加函数参数,一个节点做不到,我们就给他安排两个节点

AC代码

void connectLeftRight(Node* node1,Node* node2){
       if(node1==NULL || node2==NULL ) return;
       
       node1->next=node2;
       connectLeftRight(node1->left,node1->right);
       connectLeftRight(node2->left,node2->right);
       connectLeftRight(node1->right,node2->left);
   }
    
    Node* connect(Node* root) {
        if(root==NULL)  return root;
        connectLeftRight(root->left,root->right);
        return root;
  }

114. Flatten Binary Tree to Linked List

题目链接:https://leetcode.com/problems/flatten-binary-tree-to-linked-list/

AC代码:

还是二叉树 ,递归思想 相信这个定义,让root做它该做的事情,然后flatten函数就会按照定义工作。

 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;
        
        //将原来的右子树接到现在的右子树右边
        TreeNode* p=root;
        while(p->right!=NULL){
            p=p->right;
        }
        p->right=right;
        
    }

654. Maximum Binary Tree

note: 1、TreeNode* root=new TreeNode(1)   新建节点时候注意

2、 边界条件是left<right 不是=

3、vector 通过增加参数left right建树

AC代码

 TreeNode* consturctTree(vector<int>& nums,int left,int right){
        //
        if(left>right) return NULL;
        int max=left;
        for(int i=left+1;i<=right;i++){
            if(nums[i]>nums[max]) max=i;
        }
        TreeNode* root=new TreeNode(nums[max]);
        root->left=consturctTree(nums,left,max-1);
        root->right=consturctTree(nums,max+1,right);
        return root;
    }
    
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
      return consturctTree(nums,0,nums.size()-1);   
    }

 105. Construct Binary Tree from Preorder and Inorder Traversal

思路:寻找左子树的前序遍历结尾  关键!!通过leftSize

TreeNode*constrctTree(vector<int>& preorder, int pl,int pr,vector<int>& inorder,int il,int ir){
        //边界
        if(pl>pr || il>ir)  return NULL;
         TreeNode *root=new TreeNode(preorder[pl]); 
        //寻找中序遍历中根节点的index
        int rootval=preorder[pl];
        int index;
         for(int i=il;i<=ir;i++){
             if(inorder[i]==rootval) {
                 index=i;
                 break;
             }
         }
        
        //寻找左子树的前序遍历结尾  关键!!通过leftSize
        //左子树前序遍历结尾=pl+1+leftSize-1
        int leftSize=index-il;
        
        root->left=constrctTree(preorder,pl+1,pl+leftSize,inorder,il,index-1);
        root->right=constrctTree(preorder,pl+leftSize+1,pr,inorder,index+1,ir);
        return root;
    }
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        return constrctTree(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1);
    }

106. Construct Binary Tree from Inorder and Postorder Traversal

思路:寻找子树的后序遍历区间 关键还是!!通过中序找到leftSize

AC代码:

 TreeNode* constructTree(vector<int>& inorder,int inStart,int inEnd,
                   vector<int>& postorder,int postStart,int postEnd){
          //边界
          if(inStart>inEnd || postStart>postEnd){
              return NULL;
          }
          int rootVal=postorder[postEnd];
          TreeNode *root=new TreeNode(rootVal);
          int index;
          for(int i=0;i<inorder.size();i++){
              if(inorder[i]==rootVal){
                  index=i;
                  break;
              }
          }
          int leftSize=index-inStart;
          root->left=constructTree(inorder,inStart,index-1,postorder,postStart,postStart+leftSize-1);
          root->right=constructTree(inorder,index+1,inEnd,postorder,postStart+leftSize,postEnd-1);
          return root;
        }
    
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
       return constructTree(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1);
        
    }

猜你喜欢

转载自blog.csdn.net/weixin_40760678/article/details/108804757