leetcode 剑指offer重复题目

226. Invert Binary Tree    28 

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root==nullptr) return root;
        invert(root);
        return root;
    }
    void invert(TreeNode* root){
        if(root==nullptr) return;
        if(root->left==nullptr&&root->right==nullptr)
            return;
        TreeNode*temp=root->left;
        root->left=root->right;
        root->right=temp;
        invert(root->left);
        invert(root->right);
    }
};

263. Ugly Number

class Solution {
public:
    bool isUgly(int num) {
        if(num==0) return false;
        while(num%2==0)
            num/=2;
        while(num%3==0)
            num/=3;
        while(num%5==0)
            num/=5;


        return num==1;
    }
};


105. Construct Binary Tree from Preorder and Inorder Traversal  7 


155. Min Stack   30

class MinStack {
public:
    /** initialize your data structure here. */
    MinStack() {
        
    }
    
    void push(int x) {
        mains.push(x);
        if(fuzhu.size()==0||fuzhu.top()>x)
            fuzhu.push(x);
        else 
        {
            fuzhu.push(fuzhu.top());
        }
    }
    
    void pop() {
        mains.pop();
        fuzhu.pop();
    }
    
    int top() {
        return mains.top();
    }
    
    int getMin() {
        return fuzhu.top();
    }
    stack<int> fuzhu;
    stack<int> mains;
 };


225. Implement Stack using Queues 9 相关

解答:http://blog.csdn.net/m0_37693059/article/details/76422264

三种方法


232. Implement Queue using Stacks 9

class MyQueue {
public:
    /** Initialize your data structure here. */
    MyQueue() {
        
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        stack1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        if(stack2.empty())
        {
            while(!stack1.empty())
            {
                stack2.push(stack1.top());
                stack1.pop();
            }
        }
        int temp=stack2.top();
        stack2.pop();
        return temp;
    }
    
    /** Get the front element. */
    int peek() {
         if(stack2.empty())
        {
            while(!stack1.empty())
            {
                stack2.push(stack1.top());
                stack1.pop();
            }
        }
        return stack2.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return stack1.empty()&&stack2.empty();
    }
    stack<int> stack1;
    stack<int> stack2;
};

238. Product of Array Except Self 66



572. Subtree of Another Tree   26


/**
 * 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 isSubtree(TreeNode* s, TreeNode* t) {
        bool result=false;
        if(s&&t)
        {
            if(s->val==t->val)//入口点
                result=hasSub(s,t);
            if(result==false)
                result=isSubtree(s->left,t);
            if(result==false)
                result=isSubtree(s->right,t);
        }
        return result;
    }
    bool hasSub(TreeNode* owner,TreeNode* sub){
        if(owner==nullptr&&sub==nullptr) return true;
        if(owner==nullptr||sub==nullptr) return false;
        if(owner->val!=sub->val) return false;
        return hasSub(owner->left,sub->left)&&hasSub(owner->right,sub->right);
    }
};


233   43

http://blog.csdn.net/m0_37693059/article/details/76468223




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值