We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接
1.如果两个二叉树都为空,则它们相同返回 true。 2.如果两个二叉树中有一个为空,则它们不同返回 false。 3.如果两个二叉树都不为空,首先判断根节点是否相同,不同则返回 false。 4.如果两个二叉树的根节点相同,则分别递归判断其左右子树是否相同。
const isSameTree = function(p, q) { if (p === null && q === null) return true; if (p === null || q === null) return false; if (p.val !== q.val) return false; return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); };
The text was updated successfully, but these errors were encountered:
// BFS迭代法
var isSameTree = function(p, q) { if(!p && !q){ return true } let queue = [p, q]; while(queue.length){ let root1 = queue.shift(); let root2 = queue.shift(); if(!root1 && !root2) { continue; } if(root1 && root2 && root1.val !== root2.val){ return false } if(!root1 || !root2){ return false } queue.push(root1.left, root2.left); queue.push(root1.right, root2.right); } return true; }
Sorry, something went wrong.
No branches or pull requests
原题链接
深度优先搜索 DFS
1.如果两个二叉树都为空,则它们相同返回 true。
2.如果两个二叉树中有一个为空,则它们不同返回 false。
3.如果两个二叉树都不为空,首先判断根节点是否相同,不同则返回 false。
4.如果两个二叉树的根节点相同,则分别递归判断其左右子树是否相同。
The text was updated successfully, but these errors were encountered: