leetCode 100. Same Tree 树
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
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。