100. Same Tree


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.

题目大意:

判断两个二叉树是否完全相同。包括他们节点的内容。

代码如下:(递归版)

/***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*TreeNode*right;*TreeNode(intx):val(x),left(NULL),right(NULL){}*};*/classSolution{public:boolisSameTree(TreeNode*p,TreeNode*q){boolchildResult;if(NULL==p&&NULL==q)returntrue;if(NULL!=p&&NULL!=q&&p->val==q->val){returnchildResult=isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);}returnfalse;}};

2016-08-07 00:01:38