The sword refers to offer24-25 problem-solving ideas and codes (paths that sum to a certain value in a binary tree, copying of complex linked lists)

24 A path in a binary tree that sums to a certain value
class Solution {
public:
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        vector<vector<int>> answer;
        if(root->val>expectNumber)
            return answer;
        if(root->left){
            int sum=root->val;
            vector<int> left=FindNumb(root->left,expectNumber,sum);
        }
    }
    vector<int> FindNumb(TreeNode* root,int expectNumber,int sum){
        vector<int> left;
        sum+=root->val;
        if(sum>expectNumber)
            return left;
        if(sum<expectNumber){
            left.push_back(root->val);
            if(root->left)
                vector<>
        }
    }
};
I wrote a paragraph first, and then I found that I couldn’t write it anymore. It seems that the reference answer is not a big problem. The main problem is that the tree is not a search tree, so it is meaningless to judge greater than or less than. It only needs to judge whether there is a left subtree and a right subtree. subtrees and whether they are equal. code show as below:
class Solution {
private:
    vector<vector<int>> answer;
    vector<int> path;
public:
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        if(root->val>expectNumber||root==NULL)
            return answer;
        path.push_back(root->val);
        if(expectNumber-root->val==0&&root->left==NULL&&root->right==NULL){
            answer.push_back(path);
        }
        if(root->left)
        FindPath(root->left,expectNumber-root->val);
        if(root->right)
            FindPath(root->right,expectNumber-root->val);
        if(path.size()!=0)
            path.pop_back();
        return answer;
    }

};


25 Complex linked lists have to be copied
The structure of the program is relatively long, mainly by copying the linked list first, and pasting it on the edge of the linked list to form the AA1BB1 type. The second step is to copy the pointers of its complex nodes, and finally split the linked list into the original linked list. and the newly formed linked list. It is easy to cause problems if the structure is long. Sometimes there are problems with the position of brackets and some statements are wrong. It is impossible to find where the problem is. A sentence RandomListNode* pCloned=new RandomListNode(pNode->label); The pNode is written as pCloned. Array out of bounds even found me for half an hour. . .
class Solution {
public:
     RandomListNode* Clone(RandomListNode* pHead){
            CloneNode (pHead);
            CloneRandomNode (pHead);
            return ReClone (pHead);
    }
    void CloneNode(RandomListNode* pHead){
        RandomListNode * pNode = pHead;
        while(pNode!=NULL){
            RandomListNode* pCloned=new RandomListNode(pNode->label);
            pCloned->next=pNode->next;
            pCloned->random=NULL;
            pNode->next=pCloned;
            pNode=pCloned->next;
        }
    }
    void CloneRandomNode (RandomListNode * pHead) {
        RandomListNode * pNode = pHead;
        while(pNode){
            RandomListNode* pNext=pNode->next;
            if(pNode->random!=NULL)
            pNext->random=pNode->random->next;
            pNode=pNext->next;
        }
    }
        RandomListNode* ReClone(RandomListNode* pHead){
            RandomListNode * pNode = pHead;
            RandomListNode* pClonedHead=NULL;
            RandomListNode* pClonedNode=NULL;
            if(pNode){
                pClonedHead=pClonedNode=pNode->next;
                pNode->next=pClonedNode->next;
                pNode=pNode->next;
            }
            while(pNode){
                pClonedNode->next=pNode->next;
                pClonedNode=pNode->next;
                pNode->next=pClonedNode->next;
                pNode=pClonedNode->next;
            }
            return pClonedHead;
    }
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763890&siteId=291194637