[leetcode]刷题笔记-剑指offer系列

二叉树

二叉树的定义

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

1.二叉树的镜像

//执行出错
class Solution {
public:
    TreeNode* mirrorTree(TreeNode* root) {
        if(root->left!=NULL)
            mirrorTree(root->left);
        if(root->right!=NULL)
            mirrorTree(root->right);
        TreeNode *temp = root->left;
        root->left = root->right;
        root->right = temp;
        return root;
    }
};
//AC
class Solution {
public:
    TreeNode* mirrorTree(TreeNode* root) {
        if(root){
             swap(root->left,root->right);
        }else{
            return root;
        }
        mirrorTree(root->left);
        mirrorTree(root->right);
        return root;
    }
};

一定要先判断root为NULL的情况。

2.二叉树的深度

//AC
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==NULL)
            return 0;
        int lLength = maxDepth(root->left);
        int rLength = maxDepth(root->right);
        return lLength>rLength? lLength+1:rLength+1;
    }
};

经典的递归。

3.平衡二叉树

这个题一拿到手没有什么思路,只能从题目的定义逐步分析。
有两个关键点:

  1. 左右子树的深度相差不能大于1;
  2. 对于任意节点,均要满足这一条件。
//AC
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if(root==NULL)
            return true;
        if(!isBalanced(root->left))
            return false;
        if(!isBalanced(root->right))
            return false;
        int lDepth = depth(root->left),rDepth = depth(root->right);
        if(abs(lDepth-rDepth)>1)
            return false;
        return true;
    }
    int depth(TreeNode *root){
        if(root==NULL)
            return 0;
        int lLength = depth(root->left);
        int rLength = depth(root->right);
        return lLength>rLength? lLength+1:rLength+1;
    }
    int abs(int a){
        return a>0?a:-a;
    }
};

上述搜索的方法产生了对于深度的重复计算。降低时间复杂度需要剪枝。

挖坑:对上述解法用剪枝进行优化。

4.对称的二叉树

//AC
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root==NULL)
            return true;
        return isSymmetric(root->left,root->right);
    }
    bool isSymmetric(TreeNode* left,TreeNode* right){
        if(left==NULL && right==NULL) //均为空
            return true;
        if(left==NULL || right==NULL)
            return false;
        if(left->val != right->val) //容易漏,只有值相等时,才去判断子树
            return false;
        return isSymmetric(left->left,right->right) && isSymmetric(left->right,right->left);  //画图理解
    }
};

数组

1.0~n-1中缺失的数字

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int length = nums.size();
        for(int i = 0;i<length;i++){
            if(i!=nums[i])
                return i;
        }
        return length;
    }
};
//执行用时 :44 ms, 在所有 C++ 提交中击败了26.99%的用户。

效率太低。
解题思路:排序数组中的搜索问题,首先要想到 二分法 解决。

2.每日温度

//超时
class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& T) {
        int _size = T.size();
        vector<int> res(_size,0);
        for(int i = 0;i < _size-1;i++){
            for(int j = i+1;j < _size;j++){
                if(T[j]>T[i]){
                    res[i] = j-i;
                    break;
                }
            }
        }
        return res;
    }
};
//单调栈
class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& T) {
        int _size = T.size();
        vector<int> res(_size,0);
        stack<int> _stack;  //存 等待后续温度超过该日 的当前日期
        for(int i = 0;i < _size-1;i++){
            if(T[i+1] <= T[i]){
                _stack.push(i);
                continue;
            }
            res[i] = 1;
            while(!_stack.empty()){
                int _top = _stack.top();
                if(T[i+1]<= T[_stack.top()])   //
                    break;
                _stack.pop();
                res[_top] = i+1 - _top;
            }     
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值