Rrui的Leetcode刷题笔记(三)

101. 对称二叉树

/**
 * 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 isSymmetric(TreeNode* root) {
        if(!root) return true;
        stack<TreeNode*> s;
        s.push(root->left);
        s.push(root->right);
        while(!s.empty()){
            auto p = s.top();
            s.pop();
            auto q = s.top();
            s.pop();
            if(!q&&!p) continue;
            if(!p||!q) return false;
            if(p->val!=q->val) return false;
            s.push(p->left);
            s.push(q->right);
            s.push(p->right);
            s.push(q->left);
        }
        return true;
    }
};

102. 二叉树的层次遍历

/**
 * 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:
    vector<vector<int>> levelOrder(TreeNode* root) {
        
        TreeNode* p=root;
        vector<vector<int>> a;       
        queue<TreeNode*> q;            
        q.push(p);
        
        if(!p)
            return a;
        
        while(!q.empty())
        {
            int size=q.size();
            vector<int> b;  
            while(size--)
            {
                p=q.front();
                if(p->left)
                    q.push(p->left);
                if(p->right)
                    q.push(p->right);
                b.push_back(p->val);
                q.pop();
            }
            a.push_back(b);  
        }
        return a;
    }
};


103. 二叉树的锯齿形层次遍历

/**
 * 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:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        
        queue<TreeNode*> q;
        stack<TreeNode*> s;
        vector<int> m;
        vector<vector<int>> n;
        int size;
        int p=0;
        if(!root)
            return n;
        
        q.push(root);
        
        while(!q.empty())
        {
            TreeNode*a;
            
            size=q.size();
            while(size--)
            {
                a=q.front();
                if(a->left)
                    q.push(a->left);
                if(a->right)
                    q.push(a->right);
                m.push_back(a->val);
                q.pop();
            }
            p++;
            if(p%2==0)
                reverse(m.begin(),m.end());
            n.push_back(m);
            m.clear();
        }
        
        
        return n;
    }
};

104. 二叉树的最大深度

/**
 * 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:
    int maxDepth(TreeNode* root) {
        
        TreeNode* p=root;
        vector<vector<int>> a;       
        queue<TreeNode*> q;            
        q.push(p);
        
        if(!p)
            return 0;
        
        while(!q.empty())
        {
            int size=q.size();
            vector<int> b;  
            while(size--)
            {
                p=q.front();
                if(p->left)
                    q.push(p->left);
                if(p->right)
                    q.push(p->right);
                b.push_back(p->val);
                q.pop();
            }
            a.push_back(b);  
        }
        int kaka=a.size();       
        return kaka;         
    }
};

107. 二叉树的层次遍历 II

/**
 * 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:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        
        TreeNode* p=root;
        vector<vector<int>> a;       
        queue<TreeNode*> q;            
        q.push(p);
        
        if(!p)
            return a;
        
        while(!q.empty())
        {
            int size=q.size();
            vector<int> b;  
            while(size--)
            {
                p=q.front();
                if(p->left)
                    q.push(p->left);
                if(p->right)
                    q.push(p->right);
                b.push_back(p->val);
                q.pop();
            }
            a.push_back(b);

        }
        reverse(a.begin(),a.end());
        return a;        
    }
};

136. 只出现一次的数字

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        map<int, int> m;
        for (int i = 0; i < nums.size(); i++) {
            // key 为数组中的值  value为数组中的值出现的次数
            if (!m.count(nums[i])) {
                m[nums[i]] = 1;
            }else {
                m[nums[i]] += 1;
            }
        }
        for (int i = 0; i < nums.size(); i++) {
            if (m[nums[i]] ==  1) {
                return nums[i];
            }
        }
        return -1;
    }
};

144. 二叉树的前序遍历

/**
 * 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:
    vector<int> preorderTraversal(TreeNode* root) {
                
        TreeNode*h= root;
        vector<int> q;
        stack<TreeNode*> a; 
        int k;
        TreeNode*p;
        
        if(!root)
            return q;
        
        a.push(h);
   
        while(!a.empty())
        {
            p=a.top();
            a.pop();
            q.push_back(p->val);               
            if(p->right)
                a.push(p->right); 
            if(p->left)
                a.push(p->left);
        }
        return q;        
    }
};

145. 二叉树的后序遍历

/**
 * 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:
    vector<int> postorderTraversal(TreeNode* root) {
        
        TreeNode*h= root;
        vector<int> q;
        stack<TreeNode*> a; 
        int k;
        TreeNode*p;
        
        if(!root)
            return q;
        
        a.push(h);
   
        while(!a.empty())
        {
            p=a.top();
            if(p->left)
            {
                a.push(p->left);
                p->left=NULL;
            }
            else
            {
                if(p->right)
                {
                    a.push(p->right);
                    p->right=NULL;
                } 
                else
                {
                    q.push_back(p->val);                    
                    a.pop();
                }
            }

        }
        return q;       
    }
};

150. 逆波兰表达式求值

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<int> s_num;
        int num1,num2;
        for(auto token:tokens){
            if(token=="+"||token=="-"||token=="*"||token=="/"){
                num2 = s_num.top();
                s_num.pop();
                num1 = s_num.top();
                s_num.pop();
                char c = token[0];
                switch(c){
                    case '+':
                        s_num.push(num1+num2);
                        break;
                    case '-':
                        s_num.push(num1-num2);
                        break;
                    case '*':
                        s_num.push(num1*num2);
                        break;
                    case '/':
                        s_num.push(num1/num2);
                        break;
                }
            }
            else{
                s_num.push(atoi(token.c_str()));
            }       
        }
        return s_num.top();
    }
};

猜你喜欢

转载自blog.csdn.net/Rrui7739/article/details/82909872
今日推荐