我需要检查两个给定的二叉树是否相同。这是我写的一个迭代解决方案:
/**
* 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) {
const nodePair = [p, q]
const nodes = []
if(p && q) nodes.push(nodePair)
else if (!p && !q) return true
else return false
while(nodes.length) {
const lastNodePair = nodes.pop()
if(
(lastNodePair[0].val !== lastNodePair[1].val)
|| (lastNodePair[0].left && !lastNodePair[1].left)
|| (!lastNodePair[0].left && lastNodePair[1].left)
|| (lastNodePair[0].right && !lastNodePair[1].right)
|| (!lastNodePair[0].right && lastNodePair[1].right)
) return false
if(lastNodePair[0].left && lastNodePair[1].left) {
nodes.push([lastNodePair[0].left, lastNodePair[1].left])
}
else if(lastNodePair[0].right && lastNodePair[1].right) {
nodes.push([lastNodePair[0].right, lastNodePair[1].right])
}
}
return true
};
它通过了57个测试用例中的56个,但没有通过这个用例,应该是
false
:[390,255,2266,-273,337,1105,3440,-425,4113,null,null,600,1355,3241,4731,-488,-367,16,null,565,780,1311,1755,3075,3392,4725,4817,null,null,null,null,-187,152,395,null,728,977,1270,null,1611,1786,2991,3175,3286,null,164,null,null,4864,-252,-95,82,null,391,469,638,769,862,1045,1138,null,1460,1663,null,1838,2891,null,null,null,null,3296,3670,4381,null,4905,null,null,null,-58,null,null,null,null,null,null,null,null,734,null,843,958,null,null,null,1163,1445,1533,null,null,null,2111,2792,null,null,null,3493,3933,4302,4488,null,null,null,null,null,null,819,null,null,null,null,1216,null,null,1522,null,1889,2238,2558,2832,null,3519,3848,4090,4165,null,4404,4630,null,null,null,null,null,null,1885,2018,2199,null,2364,2678,null,null,null,3618,3751,null,4006,null,null,4246,null,null,4554,null,null,null,1936,null,null,null,null,2444,2642,2732,null,null,null,null,null,null,null,4253,null,null,null,null,2393,2461,null,null,null,null,4250,null,null,null,null,2537]
[390,255,2266,-273,337,1105,3440,-425,4113,null,null,600,1355,3241,4731,-488,-367,16,null,565,780,1311,1755,3075,3392,4725,4817,null,null,null,null,-187,152,395,null,728,977,1270,null,1611,1786,2991,3175,3286,null,164,null,null,4864,-252,-95,82,null,391,469,638,769,862,1045,1138,null,1460,1663,null,1838,2891,null,null,null,null,3296,3670,4381,null,4905,null,null,null,-58,null,null,null,null,null,null,null,null,734,null,843,958,null,null,null,1163,1445,1533,null,null,null,2111,2792,null,null,null,3493,3933,4302,4488,null,null,null,null,null,null,819,null,null,null,null,1216,null,null,1522,null,1889,2238,2558,2832,null,3519,3848,4090,4165,null,4404,4630,null,null,null,null,null,null,1885,2018,2199,null,2364,2678,null,null,null,3618,3751,null,4006,null,null,4246,null,null,4554,null,null,null,1936,null,null,null,null,2444,2642,2732,null,null,null,null,null,null,null,4253,null,null,null,null,2461,2393,null,null,null,null,4250,null,null,null,null,2537]
如果可能的话,我想坚持使用迭代解决方案。
最佳答案
您只比较两棵树的左子代或两棵树的右子代,因为在比较子代时在其中有else if
。删除else
。因为,如果两个树中的this.left
和this.right
都不为空,则不会比较正确的子级,因为那里有else
。只比较剩下的孩子
if(lastNodePair[0].left && lastNodePair[1].left) {
nodes.push([lastNodePair[0].left, lastNodePair[1].left])
}
if(lastNodePair[0].right && lastNodePair[1].right) {
nodes.push([lastNodePair[0].right, lastNodePair[1].right])
}
关于javascript - 同一树,迭代解决方案,JavaScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52731182/