Given two binary trees (defined structure in C++ as follows), check if they are identical (same tree structure and the values to the nodes are also the same):
1 2 3 4 5 6 7 8 9 | /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ |
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */
To solve this problem, there are three steps. First, to check the current node value, and check the left tree (if any) and the last would be to check the right tree (if any).
Therefore, this can be clearly solved in recursion in a straightforward coding style.
1 2 3 4 5 6 7 8 9 10 11 12 | class Solution { public: bool isSameTree(TreeNode *p, TreeNode *q) { // empty nodes are equal if ((p == NULL) && (q == NULL)) return true; // structure not equal (one of them is empty) if (p == NULL) return false; if (q == NULL) return false; // value must be the same and two sub trees must be the same return (p->val == q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } }; |
class Solution { public: bool isSameTree(TreeNode *p, TreeNode *q) { // empty nodes are equal if ((p == NULL) && (q == NULL)) return true; // structure not equal (one of them is empty) if (p == NULL) return false; if (q == NULL) return false; // value must be the same and two sub trees must be the same return (p->val == q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } };
I personally believe that recursion (despite its performance disadvantage) is well fit into solving the tree-related problems because it is very straightforward in describing the algorithms.
Javascript Function to Check if Two Binary Trees are Same
The above implementation can be easily translated to Javascript.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} p * @param {TreeNode} q * @return {boolean} */ var isSameTree = function(p, q) { if (p === null && q === null) return true; if (p === null || q === null) return false; return (p.val === q.val) && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); }; |
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} p * @param {TreeNode} q * @return {boolean} */ var isSameTree = function(p, q) { if (p === null && q === null) return true; if (p === null || q === null) return false; return (p.val === q.val) && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); };
The parameters p and q are two binary trees and the JS function will return if p and q are the same.
Using Stack to Check Two Binary Trees for Equality Iteratively
The recursion can be re-written using a stack. We push a pair of TreeNodes of both Binary Trees, then we check for equality for two nodes, and push their children nodes to the stack until the stack is empty (then the both trees are same), or some nodes are not equal then we return false immediately.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { stack<pair<TreeNode*, TreeNode*>> st; st.push({p, q}); while (!st.empty()) { auto p = st.top(); st.pop(); auto left = p.first; auto right = p.second; if (left && right) { if (left->val != right->val) { return false; } st.push({left->left, right->left}); st.push({left->right, right->right}); } else { if ((left != nullptr) ^ (right != nullptr)) { return false; } } } return true; } }; |
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { stack<pair<TreeNode*, TreeNode*>> st; st.push({p, q}); while (!st.empty()) { auto p = st.top(); st.pop(); auto left = p.first; auto right = p.second; if (left && right) { if (left->val != right->val) { return false; } st.push({left->left, right->left}); st.push({left->right, right->right}); } else { if ((left != nullptr) ^ (right != nullptr)) { return false; } } } return true; } };
We can see that the Recursive implementation is simpler to implement and concise.
Same Binary Tree Check Algorithm:
- How to Check If Two Binary Trees are the Same?
- Teaching Kids Programming – Recursive Depth First Search Algorithm to Check If Two Binary Trees are Same
- GoLang: Check Same Binary Trees via DFS or BFS Algorithms
- Teaching Kids Programming – Breadth First Search Algorithm to Check If Two Binary Trees are Same
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: Coding Exercise - Climbing Stairs - Fibonacci Numbers - C++ - Online Judge
Next Post: Coding Exercise - Remove Duplicates From Sorted List - C++ - Online Judge