mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 02:53:31 +08:00
@ -253,6 +253,28 @@ var isSameTree = function (p, q) {
|
||||
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
|
||||
};
|
||||
```
|
||||
> 迭代法
|
||||
|
||||
```javascript
|
||||
var isSameTree = (p, q) => {
|
||||
const queue = [{ p, q }];
|
||||
// 这是用{ } 解决了null的问题!
|
||||
while (queue.length) {
|
||||
const cur = queue.shift();
|
||||
if (cur.p == null && cur.q == null) continue;
|
||||
if (cur.p == null || cur.q == null) return false;
|
||||
if (cur.p.val != cur.q.val) return false;
|
||||
queue.push({
|
||||
p: cur.p.left,
|
||||
q: cur.q.left
|
||||
}, {
|
||||
p: cur.p.right,
|
||||
q: cur.q.right
|
||||
});
|
||||
}
|
||||
return true;
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
|
Reference in New Issue
Block a user