LeetCode 100 Same Tree (Same Tree judgment) (Binary Tree, recursion, stack and queue, deep search and wide search)
Translation
Given two binary trees, write a function to check whether they are equal. If the two binary trees have the same structure and have the same value, they are considered to be equal.
Original
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Analysis
It is better to use recursion that has always been used.
In terms of structure consistency, tree A has A left node, and tree B also has A left node. This can be used to determine at one time:
if ((node1->left && node2->left) && (node1->right && node2->right)) {}
However, you cannot determine whether a node exists at the same time. if the node does not exist but the value is calculated later, the problem may occur. Therefore, it is better to honestly use several if statements to determine whether the node exists.
bool isSameNode(TreeNode *node1, TreeNode *node2) { if (node1->val == node2->val) { bool b1 = false, b2 = false; TreeNode *temp1 = node1->left; TreeNode *temp2 = node2->left; if (temp1 != NULL && temp2 != NULL) { b1 = isSameNode(temp1, temp2); } else if (temp1 == NULL && temp2 == NULL) { b1 = true; } TreeNode *temp3 = node1->right; TreeNode *temp4 = node2->right; if (temp3 != NULL && temp4 != NULL) { b2 = isSameNode(temp3, temp4); } else if (temp3 == NULL && temp4 == NULL) { b2 = true; } return b1 && b2; } else { return false; }}bool isSameTree(TreeNode *p, TreeNode *q) { if (p != NULL && q != NULL) return isSameNode(p, q); else if (p == NULL && q == NULL) return true; else return false;}
Then we will continue to compress and compress the above. For a and B nodes, there are only five situations:
| Node |
Node B |
Related code |
| Null |
Null |
If (p = NULL & q = NULL) return true; |
| Not empty |
Null |
Else if (p = NULL or q = NULL) return false; |
| Null |
Not empty |
Else if (p = NULL or q = NULL) return false; |
| Not empty |
Non-empty (value does not want to wait) |
Else if (p-> val! = Q-> val) return false; |
| Not empty |
Non-null (equal value) |
Else isSameTree (p-> left, q-> left) & isSameTree (p-> right, q-> right ); |
Note: Due to the format of the Markdown table, the "|" of the second and third lines of code are both expressed by or.
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->val) return false; else isSameTree(p->left, q->left) && isSameTree(p->right, q->right);}
Code
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/class Solution {public: bool isSameTree(TreeNode *p, TreeNode *q) { if (p == NULL && q == NULL) return true; else if ((p == NULL || q == NULL) || (p->val != q->val)) return false; else isSameTree(p->left, q->left) && isSameTree(p->right, q->right); }};
Advanced
I still cannot write it out. Let's see what the gods wrote. Continue to worship and continue to cheer!
DFS + stack
bool isSameTree(TreeNode *p, TreeNode *q) { stack
> myStack; myStack.push(pair
(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
(p->left, q->left)); myStack.push(pair
(p->right, q->right)); } } return myStack.empty();}
BFS + queue
bool isSameTree(TreeNode* p, TreeNode* q) { queue
> myQueue; myQueue.push(pair
(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
(p->right, q->right)); myQueue.push(pair
(p->left, q->left)); } } return myQueue.empty();}