剑指offer(七)

31. 二叉树中和为某一值的路径

class Solution {
public:
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        vector<vector<int> > result;
        vector<int> branchint;
        if(root==NULL || expectNumber<root->val) return result;

        find(root, expectNumber, branchint, result);
        return result;

    }
private:
    void find(TreeNode* root,int expectNumber,vector<int>& branchint, vector<vector<int> >& result){
        if(root==NULL) return;
        branchint.push_back(root->val);
        bool isLeaf=root->left==NULL && root->right==NULL;
        if(expectNumber==root->val && isLeaf){
            result.push_back(branchint);
        }
        else{
            find(root->left,expectNumber-root->val, branchint, result);
            find(root->right,expectNumber-root->val, branchint, result);        
        }
        branchint.pop_back();
             
    }
};

32. 复杂链表的复制

比较复杂,分三步做

/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead)
    {
        if(pHead==NULL) return NULL;
        
        newNodeInsert(pHead);
        cloneRandom(pHead);
        
        //再遍历一次,分割出来,
        //正确来说,不是把新链表分割出来,而是把旧链表的节点都删掉,否则会返回空;
        RandomListNode* resultHead=pHead->next;
        RandomListNode* result;   
        RandomListNode* Headin=pHead;

        while(Headin->next!=NULL){ 
            result=Headin->next;
            Headin->next=result->next;
            Headin=result; 
        }
        return resultHead;
        
    }
private:
    void newNodeInsert(RandomListNode*& pHead){
        //将原链表节点复制一遍并插入到原节点后
        RandomListNode* Head=pHead;
        while(Head!=NULL){
            RandomListNode* Headin=new RandomListNode(Head->label);
            Headin->next=Head->next;
            Head->next=Headin;
            Head=Headin->next;
        }
    }
    
    void cloneRandom(RandomListNode*& pHead){
        //复制任意指针
        RandomListNode* Head=pHead;
        while(Head!=NULL){
            RandomListNode* Headin=Head->next;
            if(Head->random)
                Headin->random=Head->random->next;
            Head=Headin->next;
        }
    }
    
};

33. 二叉搜索树与双向链表

中序遍历,设置pre保存每个节点的前一个节点

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    TreeNode* Convert(TreeNode* pRootOfTree)
    {
        if(pRootOfTree==NULL) return NULL;
        //
        TreeNode* pre=NULL;
        middle(pRootOfTree, pre);
        while(pre!=NULL & pre->left!=NULL) 
            pre=pre->left;
        return pre;
        
    }
private:
    void middle(TreeNode* pRootOfTree,TreeNode*& pre){
        if(!pRootOfTree) return;
 
        middle(pRootOfTree->left, pre);
        pRootOfTree->left=pre;
        if(pre)
            pre->right=pRootOfTree;
        
        pre=pRootOfTree;
        middle(pRootOfTree->right, pre);
    }
};

34. 序列化二叉树

class Solution {
public:
    char* Serialize(TreeNode *root) {    
        string str("");
        serialize(root, str);
        char* res=new char[str.length()+1];
        strcpy(res,str.c_str());
        return res;

    }
    TreeNode* Deserialize(char *str) {
        TreeNode* root=NULL;
        //char 转 string
        string s(str);
        deserialize(root,s);
        return root;
    }
private:
    void serialize(TreeNode *root, string& str){
        if(!root) str+="$,";
        else{
           str += to_string(root->val)+','; 
           serialize(root->left, str);
           serialize(root->right, str); 
        }
        return;
    }
    void deserialize(TreeNode*& root,string& str){
        if(str[0]=='$') {
        	//找到第一个',',从后面一个开始到最后作为新的字符串
            str=str.substr(str.find_first_of(',')+1);
            root=NULL;
            return;
        }
        //stoi(str) 将字符串前部分可以识别为整数的转化为数,到不可识别的字符会自动停止
        root=new TreeNode(stoi(str));
        str=str.substr(str.find_first_of(',')+1);
        deserialize(root->left,str);
        deserialize(root->right,str);
    }
};

35. 字符串的排列

class Solution {
public:
    vector<string> Permutation(string str) {
        vector<string> res;
        if(str.size()==0) return res;
        res.push_back(str);
        if(str.size()==1) return res;
        permutationHelper(str,res,0);
        
        return res;
    }
private:
    void permutationHelper(string str, vector<string>& res, int index){
        string temp;
        if(index>=str.size()-1) return;
        permutationHelper(str,res,index+1);
        for(int j=index+1;j<str.size();j++){
            temp=str;
            //去重
            if(temp[index]==temp[j])
                continue;
            //插入当前点,非交换
            for(int i=j;i>index;i--)
                temp[i]=temp[i-1];
            temp[index]=str[j];
            res.push_back(temp);
            permutationHelper(temp,res,index+1);
        }
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章