Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null)
return true;
if ((p == null && q != null) || (p != null && q == null)) {

这篇博客讨论了LeetCode第100题的解决方案,涉及如何判断两棵二叉树是否结构相同且节点值相等。作者提到,通过递归方法可以解决此问题,并指出在递归过程中对子树的判断实际上可以简化。
订阅专栏 解锁全文
9348

被折叠的 条评论
为什么被折叠?



