C + + Brush title (30/100)

Source: Internet
Author: User

Topic One: Merging two sorted lists

Input two monotonically increasing list, output two list of linked lists, of course, we need to synthesize the linked list to meet the monotone non-reduction rules.

Idea: Use two pointers CUR1,CUR2 to refer to the current smaller and larger nodes in the two linked lists, if cur1->val>cur2->val, then swap the two

/*struct ListNode {int val;    struct ListNode *next; ListNode (int x): Val (x), Next (NULL) {}};*/classSolution { Public: ListNode* Merge (listnode* pHead1, listnode*pHead2) {ListNode* Pre =NULL; ListNode* Next =NULL; if(phead1==null| | phead2==NULL) {            returnPhead1!=null?phead1:phead2; } ListNode* head = Phead1->val<phead2->val?phead1:phead2; ListNode* Cur1 = Head==phead1?phead1:phead2; ListNode* Cur2 = Head==phead1?Phead2:phead1;  while(cur1!=null&&cur2!=NULL) {            if(cur1->val<=cur2->val) {Pre=Cur1; Cur1= cur1->Next; }Else{Next=Cur1; Pre->next =CUR2; Pre=CUR2; Cur1=CUR2; CUR2=Next; }} Pre->next = Cur1!=null?CUR1:CUR2; returnHead; }};

Topic Two: substructure of the tree

Enter two binary trees, a, B, to determine whether a is a sub-structure of a. (PS: We agree that an empty tree is not a sub-structure of any tree)

Idea: Judge B is a sub-structure, so each judgment is to determine a node Suba and b root node can find an equal tree, this equality is conditional, when the node of B traversal to empty, if the Suba is not empty, then it is equal.

So write a function that determines if it is "equal", and then let each node of a get into the function comparison with the root node of B.

/*struct TreeNode {int val;    struct TreeNode *left;    struct TreeNode *right; TreeNode (int x): Val (x), left (null), right (null) {}};*/classSolution { Public:    BOOLIsEqual (treenode* pRoot1, treenode*PRoot2) {        if(Proot2==null)return true ; if(Proot1==null)return false ; if(Proot1->val!=proot2->val)return false ; if(IsEqual (Proot1->left,proot2->left) &&isequal (proot1->right,proot2->right))return true ; return false ; }    BOOLHassubtree (treenode* pRoot1, treenode*PRoot2) {        if(proot2==null| | Proot1==null)return false ; if(IsEqual (Proot1,proot2))return true ; returnHassubtree (proot1->left,proot2) | | Hassubtree (proot1->Right,proot2); }};

Topic 3:2 Fork Tree Mirror

Operates a given two-fork tree and transforms it into a mirror of the source binary tree. Input Description:
Image definition of binary tree: source binary tree         8       /6 x        /     \     5  7 9    Mirror binary tree        8       /        10   6     /\  /     9 7  5
Idea: is simply the right and left child exchange, recursive implementation is good
/*struct TreeNode {int val;    struct TreeNode *left;    struct TreeNode *right; TreeNode (int x): Val (x), left (null), right (null) {}};*/classSolution { Public:    voidMirror (TreeNode *proot) {        if(Proot==null)return ; Mirror (Proot-Left ); Mirror (Proot-Right ); TreeNode*temp = proot->Left ; Proot->left = proot->Right ; Proot->right =temp; }};



C + + Brush title (30/100)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.