Leetcode_BinaryTree合集

目录

101. 对称二叉树

100. 相同的树 

104. 二叉树的最大深度

102. 二叉树的层序遍历 

101. 对称二叉树

92% 100% 

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool ans;
    void dfs(TreeNode *Left,TreeNode *Right){
        if(!ans) return ;
        if(Left==NULL && Right==NULL){
            ans=true; return ; 
        } 
        if(Left==NULL || Right==NULL)  {ans=false; return ;}
        if(Left->val!=Right->val){
            ans=false; return ;
        }
        dfs(Left->left,Right->right);
        dfs(Left->right,Right->left);


    }


    bool isSymmetric(TreeNode* root) {
        if(root==NULL) return true;
        ans=true;
        dfs(root->left,root->right);
        return ans;
    }
};

100. 相同的树 

跟上个题一样,就改了2行代码

100% 100%  

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    bool ans;
    void dfs(TreeNode *Left,TreeNode *Right){
        if(!ans) return ;
        if(Left==NULL && Right==NULL){
            ans=true; return ; 
        } 
        if(Left==NULL || Right==NULL)  {ans=false; return ;}
        if(Left->val!=Right->val){
            ans=false; return ;
        }
        dfs(Left->left,Right->left);
        dfs(Left->right,Right->right);


    }

public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        ans=true;
        dfs(p,q);
        return ans;
    }
};

104. 二叉树的最大深度

72% 100%  

class Solution {
private:
    int cnt,tmp;
    void dfs(TreeNode *cur){    
        if(cur->left==NULL && cur->right==NULL){
            if(cnt<tmp) cnt=tmp; //cur = leaf
            return ;
        } 
        if(cur->left){
            tmp++; 
            dfs(cur->left);
            tmp--;
        }
        
        if(cur->right){
            tmp++;
            dfs(cur->right);
            tmp--;
        }
        
    }
public:
    int maxDepth(TreeNode* root) {
        if(root==NULL) return 0;
        cnt=0;tmp=1;
        dfs(root);
        return cnt;
    }
};

102. 二叉树的层序遍历 

89.22%  100.00%

ans.push_back(vector<int>()); ?

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        if(root==NULL) return {};
        queue<TreeNode *> qu;
        qu.push(root);
        int level=0;
        vector<vector<int>> ans;

        while(!qu.empty()){
            int len=qu.size();
            ans.push_back(vector<int>());
            for(int i=0;i<len;i++){
                TreeNode *cur=qu.front();qu.pop();
                ans[level].push_back(cur->val);
                 if(cur->left!=NULL){
                    qu.push(cur->left);
                }
                if(cur->right!=NULL){
                    qu.push(cur->right);
                }
            }
            level++;
        }
        return ans;
    }
};
发布了88 篇原创文章 · 获赞 77 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43107805/article/details/105318850