leetcode Same Tree

来源:互联网 发布:物联网大数据应用系统 编辑:程序博客网 时间:2024/06/18 18:07

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里创建两棵树比较,返回正确。这里一直报错。

0 0