Leetcode to same tree (same tree judgment) (binary trees, recursion, stacks and queues, deep search and wide search)

Source: Internet
Author: User

translation
给定两个二叉树,写一个函数检查他们是否相等。两个二叉树如果结构上相同并且有相同的值,那么就认定他们是相等的。
Original
writetoifequalornotequalifandthethe same value.
Analysis

It is better to use the usual recursive solution.

Structurally consistent refers to a tree has left node, B-Tree also has left node this. Using this can be judged at once:

if ((node1->left && node2->left) && (node1->right && node2->right)) {}

However, it is not possible to get the node at the same time, the subsequent evaluation of the time if the node does not exist but to find the value is going to be problematic, so still honestly use a few if to judge well.

BOOL Issamenode (TreeNode*Node1, TreeNode*Node2) {if(Node1 -Val==Node2 -val) {bool B1= false, B2= false; TreeNode*Temp1=Node1 -Left TreeNode*Temp2=Node2 -Leftif(Temp1!= NULL &&Temp2!= NULL) {B1=Issamenode (Temp1, TEMP2); }Else if(Temp1== NULL &&Temp2== NULL) {B1= true; } TreeNode*Temp3=Node1 -Right TreeNode*Temp4=Node2 -Rightif(Temp3!= NULL &&Temp4!= NULL) {B2=Issamenode (Temp3, Temp4); }Else if(Temp3== NULL &&Temp4== NULL) {B2= true; }returnB1&&B2; }Else{return false; }}bool Issametree (TreeNode*P, TreeNode*Q) {if(p!= NULL &&Q!= NULL)returnIssamenode (P, q);Else if(p== NULL &&Q== NULL)return true;Else return false;}

Then continue to compress compression, above so much nonsense, for a, b two nodes is nothing more than 5 kinds of situations:

case of Node A Case of Node B Related Code
Empty Empty if (p = = NULL && q = = null) return true;
Non-empty Empty else if (p = = NULL or q = = null) return false;
Empty Non-empty else if (p = = NULL or q = = null) return false;
Non-empty Non-empty (value does not want to wait) else if (p->val! = Q->val) return false;
Non-empty Non-null (value equal) else Issametree (P->left, Q->left) && issametree (P->right, q->right);

Additional note: Due to the format of the Markdown table, the second and third lines of code "| |" are expressed in words or characters.

BOOL Issametree (TreeNode*P, TreeNode*Q) {if(p== NULL &&Q== NULL)return true;Else if(p== NULL ||Q== NULL)return false;Else if(p -Val!=Q -Valreturn false;ElseIssametree (P -Left, q -Left&&Issametree (P -Right, Q -right);}
Code
/*** Definition forA binary tree node.* struct TreeNode {*intval;* TreeNode * Left; * TreeNode * Right; * TreeNode (intx): Val (x), Left(NULL), Right(NULL) {}* };*/classSolution { Public: bool Issametree (TreeNode *p, TreeNode *q) {if(p = =NULL&& Q = =NULL) returntrue;Else    if(p = =NULL|| Q = =NULL) || (P->val! = q->val)) Returnfalse;ElseIssametree (p-> Left, q-> Left) && Issametree (p-> Right, q-> Right); }};
Advanced

Still failed to write, see the Great God wrote, continue to worship, continue to refuel!

DFS + Stack

bool  issametree (TreeNode *p, TreeNode *q) {stack  <pair<treenode*,    Treenode*>  > Mystack;    Mystack.push (pair<treenode*, treenode*> (p, q)); while  (!mystack.empty ())        {p = mystack.top (). First;        Q = Mystack.top (). Second; if  (!p ^!q | |            (P && q && p->val! = q->val))        break ;        Mystack.pop (); if  (P && Q)            {Mystack.push (pair<treenode*, treenode*> (P->left, q->left));        Mystack.push (pair<treenode*, treenode*> (P->right, q->right));       }} return  mystack.empty ();} 

BFS + queue

bool  issametree (TreeNode* p, treenode* q) {queue  <pair<treenode*,    Treenode*>  > Myqueue;    Myqueue.push (pair<treenode*, treenode*> (p, q)); while  (!myqueue.empty ())        {p = Myqueue.front (). First;        Q = Myqueue.front (). Second; if  (!p ^!q | |            (P && q && p->val! = q->val))        break ;        Myqueue.pop (); if  (P && Q)            {Myqueue.push (pair<treenode*, treenode*> (P->right, q->right));                         Myqueue.push (pair<treenode*, treenode*> (P->left, q->left)); }} return  myqueue.empty ();}  

Leetcode same tree (same tree judgment) (binary trees, recursion, stacks and queues, deep search and wide search)

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.