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