码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 之 Same Tree

时间:2018-04-08 19:51:23      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:题目   not   turn   cti   bool   node   bin   bubuko   bsp   

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     }

 

leetcode 之 Same Tree

标签:题目   not   turn   cti   bool   node   bin   bubuko   bsp   

原文地址:https://www.cnblogs.com/wangxiaoyong/p/8746699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!