Skip to content
New issue

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

相同的树 #463

Open
Pcjmy opened this issue Feb 12, 2023 · 1 comment
Open

相同的树 #463

Pcjmy opened this issue Feb 12, 2023 · 1 comment

Comments

@Pcjmy
Copy link
Contributor

Pcjmy commented Feb 12, 2023

No description provided.

@topulikeweb
Copy link

topulikeweb commented Mar 17, 2024

不知道是不是这个意思

// 构建树

class TreeNode {
  constructor (val = 0, left = null, right = null) {
    this.val = val
    this.left = left
    this.right = right
  }
}



function isSameTree (p, q) {
  if (!q && !p) {
    return true
  }
  if (p.left === q.left) {
    return true
  }
  if (p.right === q.right) {
    return true
  }
  return isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
}


const tree1 = new TreeNode(1, new TreeNode(2), new TreeNode(3));
const tree2 = new TreeNode(1, new TreeNode(2), new TreeNode(3));

console.log(isSameTree(tree1, tree2))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants