HappyLeetcode19 same Tree

Source: Internet
Author: User

The test instructions of this problem is particularly simple, that is, to compare two two-fork trees, to determine whether the two binary trees are exactly the same. To get this problem, since it is related to the two fork tree, it is necessary to use recursion. Just a bit more familiar with recursion, unfortunately, or recursion is not too deep understanding. Causes the code to be written to fail at all times.

In fact, the idea is particularly simple.

classSolution { Public:    BOOLIssametree (TreeNode *p, TreeNode *q) {if(p = = Null&&q = =NULL)return true; if(P! = null&&q = = NULL | | p = = null&&q! =NULL)return false; returnIssametree (P->left,q->left) &&issametree (p->right,q->right) &&p->val==q->Val; }};

In reference to other people's ideas, changed and changed, all of their original ideas to change. It's almost exactly the same as the source code. The problem is that you are recursively divided into two parts. Then the end result should go with the right. This never occurred to me until I looked at someone else's code. After watching, suddenly there is a feeling of enlightened. It is very meaningful to remember this question well.

HappyLeetcode19 same Tree

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.