劍指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);
        }
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章