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.
一般树的问题,用递归方式比较容易解决:
if(p==NULL && q==NULL) return true;
else if(p==NULL) return false;
else if(q==NULL) return false;
return (p->val==q->val &&isSameTree(p->left,q->left)&& isSameTree(p->right,q->right));尝试用了非递归算法,自己vs里创建两棵树比较,返回正确。这里一直报错。
本文讨论了如何使用递归和非递归算法来判断两个二叉树是否相等。通过实现两种方法并进行比较,揭示了它们在实现细节上的差异和优缺点。
4112

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



