leetcode 之 Same Tree

1、题目描述

Given two binary trees, write a function to check if they are the same or not.  

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

 

给定两个二叉树,判断两个二叉树是否相同。

 

3、代码

 1  bool isSameTree(TreeNode* p, TreeNode* q) {
 2         
 3         if( p == NULL || q == NULL ) return ( p == q );
 4       
 5         if( p->val == q->val && isSameTree(p->left,q->left)  && isSameTree(p->right,q->right) )
 6             return true;
 7         else
 8             return false;
 9      
10     }

 

posted @ 2018-04-08 18:47  山里的小勇子  阅读(174)  评论(0编辑  收藏  举报