100. Same tree
Link: https://leetcode-cn.com/problems/same-tree/
See the link for the title description.
Solution 1: breadth first search
What took advantage of was the problem of traversing the binary tree just yesterday, so it was completed smoothly today. At the beginning, I wanted to complete the search with depth first, but the recursive return result is always a little confused. However, I remember the previous lessons. First do the simple, thoughtful, even violent algorithm solution, and then consider other solutions
Declare two temporary arrays, place the subsequent nodes of p and q respectively, and then traverse the two arrays. It is necessary to consider the categories when the nodes of the two trees are inconsistent. The idea is clear:
var isSameTree = function (p, q) { const queue1 = [p]; const queue2 = [q]; while (queue1.length || queue2.length) { const current1 = queue1.shift(); const current2 = queue2.shift(); if (current1 && !current2) { return false; } if (!current1 && current2) { return false; } if (current1 && current2) { if (current1.val !== current2.val) { return false; } queue1.push(current1.left); queue2.push(current2.left); queue1.push(current1.right); queue2.push(current2.right); } } return true; };
- Time complexity: ${O(min(m, n)} $, where m and N are the number of two binary tree nodes
- Space complexity: ${O(min(m, n)} $, where m and N are the number of two binary tree nodes
- Execution time: 76ms, beating 94% of users in all JavaScript submissions, memory consumption: 39MB, beating 24% of users in all JavaScript submissions
Solution 2: breadth first search optimization
On the basis of the above solution, we did some meaningless optimization. We declared less than one queue array and used one. It should not be called an algorithm. At best, the code should be concise
var isSameTree = function (p, q) { const queue = [p, q]; while (queue.length) { const current1 = queue.shift(); const current2 = queue.shift(); if (current1 && !current2) { return false; } if (!current1 && current2) { return false; } if (current1 && current2) { if (current1.val !== current2.val) { return false; } queue.push(current1.left); queue.push(current2.left); queue.push(current1.right); queue.push(current2.right); } } return true; };
- Time complexity: ${O(min(m, n)} $, where m and N are the number of two binary tree nodes
- Space complexity: ${O(min(m, n)} $, where m and N are the number of two binary tree nodes
- Execution time: 80ms, beating 83% of users in all JavaScript submissions, memory consumption: 38.9MB, beating 71% of users in all JavaScript submissions
Solution 3: depth first search
After the completion of breadth first, the idea of depth first is also clear. Like the official problem solution, it is possible to return the logical operation result of recursive call of two numbers. I am stupid. I declare an external variable and an internal function, and change the value of the external variable through the recursive call of the internal function (actually refer to the previous official problem solution)
var isSameTree = function (p, q) { let isSame = true; function judge(l, r) { if ((l && !r) || (!l && r)) { isSame = false; } else if (l && r) { if (l.val !== r.val) { isSame = false; } else { judge(l.left, r.left); judge(l.right, r.right); } } } judge(p, q); return isSame; };
- Time complexity: ${O(min(m, n)} $, where m and N are the number of two binary tree nodes
- Space complexity: ${O(min(m, n)} $, where m and N are the number of two binary tree nodes
- Execution time: 80ms, beating 83% of users in all JavaScript submissions, memory consumption: 38.7MB, beating 91% of users in all JavaScript submissions