wKioL1c2r5qyPRiIAACGXo0itfk305.jpg

class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
    if(pListHead==NULL||k==0){
        return NULL;
    }
    ListNode* fast=pListHead;
    ListNode* slow=pListHead;
    while(fast){
        fast=fast->next;
        if(k==0){
            slow=slow->next;
        }
        else
            --k;
    }
    if(k==0){
        return slow;
    }
    return NULL;
  }
};

wKiom1c2rrijgbEcAABFxWZ5PPg482.jpg

class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
		if(pHead==NULL)
            return NULL;
        ListNode* newHead=pHead;
        ListNode* tmp=pHead->next;
        newHead->next=NULL;
        while(tmp){
            ListNode* next=tmp->next;
            tmp->next=newHead;
            newHead=tmp;
            tmp=next;
        }
        return newHead;
    }
};

wKiom1c2s17S87N5AABsM5aEpzg241.jpg

class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        if(pHead1==NULL)
            return pHead2;
        if(pHead2==NULL)
            return pHead1;
        if(pHead1==NULL&&pHead2==NULL)
            return NULL;
        ListNode* tmp1=pHead1,*tmp2=pHead2,*tmp=NULL,*newHead=NULL;
        while(tmp1&&tmp2){
            if(tmp1->val>=tmp2->val){
                if(newHead==NULL){
                    newHead=tmp2;
                    tmp=newHead;
                }
                else
                    {
                    tmp->next=tmp2;
                    tmp=tmp->next;
                }
              	tmp2=tmp2->next;
            }
            else{
                if(newHead==NULL){
                	newHead=tmp1;
                    tmp=newHead;
                }else
                    {
                    tmp->next=tmp1;
                    tmp=tmp->next;
                }
                tmp1=tmp1->next;
            }
        }
        if(tmp2)
            tmp->next=tmp2;
        if(tmp1)
            tmp->next=tmp1;
        return newHead;
    }
};

wKioL1c2tEKh2SusAABwCZiaCys700.jpg

题意分析:1.是子结构代表2个树 数据域相同 并不是指针相同

     2.可能子树结束父树 未结束

     3.逻辑必须清楚,活用&& ,||符号 

       

wKiom1c22IaDj7aXAAAqYG2Xkf0245.png

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
    {	
        if(pRoot1==NULL||pRoot2==NULL)
            return false;
        if(pRoot1->val==pRoot2->val){
             if(hasSubtree(pRoot1, pRoot2))
                 return true;
        }
       	if(HasSubtree(pRoot1->left,pRoot2)||\
           HasSubtree(pRoot1->right, pRoot2))
            return true;
		return false;
    }
    bool hasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
  	{
        if((pRoot1==NULL&&pRoot2!=NULL))
            return false;
        if(pRoot2==NULL)
            return true;
        if(pRoot1->val==pRoot2->val)
        {
            if(hasSubtree(pRoot1->left,pRoot2->left)&&\
               hasSubtree(pRoot1->right,pRoot2->right))
                return true;
            return false;
        }
        return false;
    }
};

wKiom1c2s1-C80BvAABn7PMW7TU384.jpg

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    void Mirror(TreeNode *pRoot) {
		if(pRoot==NULL)
            return;
         TreeNode* tmp=pRoot->left;
         pRoot->left=pRoot->right;
         pRoot->right=tmp;
         Mirror(pRoot->left);
         Mirror(pRoot->right);
		 
        
    }
};

wKiom1c2s9Ojsi9YAAEq2zAWa9I242.jpg

注意:用C++ vector 创建二维数组:

    1.创建时用双重循环 

     2.操作时 写入数据必须用push_back();

    3.读取数据 可用数组方法 arr[x][x];

class Solution {

public:
	
	vector<int> printMatrix(vector<vector<int> > matrix) {
		vector<int> arr;
		if (matrix.size() == 0 || matrix[0].size() == 0)
			return arr;
		int num = matrix.size()*matrix[0].size() - 1;
		int tag = 0, flag = 0;
		int index = 0, row = 0, line = 0;
		while (1){
			if (flag == 0){
				for (; row<((int)matrix[0].size() - tag); row++){
					arr.push_back(  matrix[line][row]);
					index++;
				}
				if (index > num)
					return arr;
				flag++;
				line++;
				row--;
			}
			if (flag == 1){
				for (; line<((int)matrix.size() - tag); line++){
					arr.push_back(matrix[line][row]);
					index++;
				}
				if (index >num)
					return arr;
				flag++;
				line--;
				row--;
			}
			if (flag == 2){
				for (; row >= tag; row--){
					arr.push_back(matrix[line][row]);
					index++;
				}
				if (index > num)
					return arr;
				flag++;
				line--;
				row++;
			}

			if (flag == 3){
				for (; line>tag; line--){
					arr.push_back(matrix[line][row]);
					index++;
				}
				if (index > num)
					return arr;
				flag = 0;
				line++;
				row++;
			}
			tag++;
		}

	}
};

wKiom1c2s2CRW6JpAABTSReKkng291.jpg

class Solution {
    stack<int> _val,_min;
public:
    void push(int value) {
        _val.push(value);
        if(_min.empty()){
            _min.push(value);
        }
        else{
            if(_min.top()>value)
                _min.push(value);
        }
    }
    void pop() {
        if(!_val.empty()){
           if(!_min.empty()&&_min.top()==_val.top())
            	_min.pop();
            _val.pop();
        }
    }
    int top() {
        return 0;
    }
    int min() {
        if(!_min.empty())
        	return _min.top();
        else
            return -1;
    }
};