vlambda博客
学习文章列表

两个二叉树结构与对应节点值是否相同


算法如下:

/** * @description 二叉树结构与对应节点值是否相同 * @param {*} xTree 二叉树 x * @param {*} yTree 二叉树 y */function isEqualTree(xTree, yTree) { const stack = [] stack.push(xTree) stack.push(yTree)
while (stack.length) { const x = stack.pop(), y = stack.pop() if (!x && !y) continue if (!x || !y) return false if(x.value != y.value) return false
stack.push(x.right) stack.push(y.right) stack.push(x.left) stack.push(y.left) } return true}