leetcode刷题记录

记录一下安全菜鸡学开发的过程

Posted by X1ng on November 9, 2020

2020.11.09

100

Runtime: 4 ms, faster than 51.00% of C++ online submissions for Same Tree.

Memory Usage: 10.4 MB, less than 51.00% of C++ online submissions for Same Tree.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */

class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(p != nullptr && q != nullptr){
            if(p->val == q->val) {
                if(isSameTree(p->left, q->left))
                    if(isSameTree(p->right, q->right))
                        return true;
                    else
                        return false;
                else
                    return false;
            }
            else
                return false;
        }
        else if(p == nullptr && q == nullptr)
            return true;
        else 
            return false;
    }
};

101

Runtime: 4 ms, faster than 89.80% of C++ online submissions for Symmetric Tree.

Memory Usage: 16.8 MB, less than 81.25% of C++ online submissions for Symmetric Tree.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(p != nullptr && q != nullptr){
            if(p->val == q->val) {
                if(isSameTree(p->left, q->right))
                    if(isSameTree(p->right, q->left))
                        return true;
                    else
                        return false;
                else
                    return false;
            }
            else
                return false;
        }
        else if(p == nullptr && q == nullptr)
            return true;
        else 
            return false;
    }
    
    bool isSymmetric(TreeNode* root) {
        if(root != nullptr){
            if(root->left != nullptr && root->right != nullptr){
                TreeNode *p = root->left, *q = root->right;
                if(root->left->val == root->right->val){
                    return isSameTree(p,q);
                }
                else
                    return false;
            }
            else if(root->left == nullptr && root->right == nullptr)
                return true;
            else 
                return false;
        }
        else 
            return true;
    }
};

104

Runtime: 12 ms, faster than 66.30% of C++ online submissions for Maximum Depth of Binary Tree.

Memory Usage: 19.3 MB, less than 22.55% of C++ online submissions for Maximum Depth of Binary Tree.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    
    int go(TreeNode *node, int &deepth, int &max){
        if(node != nullptr){
            deepth ++;
            go(node->left, deepth, max);
            go(node->right, deepth, max);
            if(deepth > max)
                max = deepth;
            deepth --;
        }
        return 0;
    }
    
    int maxDepth(TreeNode* root) {
        int deepth = 0;
        int max = 0;
        if(root != nullptr)
            go(root, deepth, max);
        return max;
    }
};

107

Runtime: 4 ms, faster than 85.05% of C++ online submissions for Binary Tree Level Order Traversal II.

Memory Usage: 14 MB, less than 22.42% of C++ online submissions for Binary Tree Level Order Traversal II.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    
    void go(TreeNode* node, vector<int> *a, int &deepth)
    {
        deepth ++;
        if(node->left != nullptr && node->right != nullptr){
            go(node->left, a, deepth);
            go(node->right, a, deepth);
            a[deepth].push_back(node->left->val);
            a[deepth].push_back(node->right->val);
        }
        else if(node->left != nullptr && node->right == nullptr){
            go(node->left, a, deepth);
            a[deepth].push_back(node->left->val);
            
        }
        else if(node->left == nullptr && node->right != nullptr){
            go(node->right, a, deepth);
            a[deepth].push_back(node->right->val);
        }
        deepth --;
        return;
        
    }
    
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> out;
        vector<int> a[751];
        int deepth = 0;
        if(root!=nullptr){
            go(root, a, deepth);
            a[deepth].push_back(root->val);
            for(int i = 0; i<=750; i++){
                if(!(a[750-i].empty()))
                    out.push_back(a[750-i]);
            }
        }
        return out;
    }
};