Same Tree

Total Accepted: 97481 Total Submissions: 230752 Difficulty: Easy

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.

Subscribe to see which companies asked this question

这次卡在了递归的return使用上,原来可以一次return两个啊

错误写法:

else if(p != NULL && q != NULL && p->val == q->val) {
  isSameTree(p->left, q->left) ;

  isSameTree(p->right, q->right);
}

正确写法:

else if(p != NULL && q != NULL && p->val == q->val) {
  return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
}

这次是C语言

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
if(p == NULL && q == NULL)
return true;
else if(p != NULL && q != NULL && p->val == q->val) {
return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
}
else
return false;
}

【6_100】Same Tree的更多相关文章

  1. 【BZOJ】2631: tree LCT

    [题意]给定n个点的树,每个点初始权值为1,m次操作:1.x到y的点加值,2.断一条边并连一条边,保证仍是树,3.x到y的点乘值,4.x到y的点权值和取模.n,m<=10^5. [算法]Link ...

  2. 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并

    [BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...

  3. 【题解】Digit Tree

    [题解]Digit Tree CodeForces - 716E 呵呵以为是数据结构题然后是淀粉质还行... 题目就是给你一颗有边权的树,问你有多少路径,把路径上的数字顺次写出来,是\(m\)的倍数. ...

  4. 【题解】[P4178 Tree]

    [题解]P4178 Tree 一道点分治模板好题 不知道是不是我见到的题目太少了,为什么这种题目都是暴力开值域的桶QAQ?? 问点对,考虑点分治吧.直接用值域树状数组开下来,统计的时候直接往树状数组里 ...

  5. 【总结】Link-Cut Tree

    这是一篇关于LCT的总结 加删边的好朋友--Link Cut Tree Link-Cut Tree,LCT的全称 可以说是从树剖引出的问题 树剖可以解决静态的修改或查询树的链上信息:那如果图会不断改变 ...

  6. 【BZOJ】1468: Tree(POJ1741) 点分治

    [题意]给定带边权树,求两点距离<=k的点对数.n<=40000. [算法]点分治 [题解]对于一个区域,选择其重心x作为根,则划分出来的每棵子树都是子区域,可以证明至多划分log n次( ...

  7. 【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)

    [Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For exam ...

  8. 【Leetcode】【Easy】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  9. 【leetcode】Symmetric Tree

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

随机推荐

  1. c#版在pc端发起微信扫码支付

    等了好久,微信官方终于发布了.net的demo. 主要代码: /** * 生成直接支付url,支付url有效期为2小时,模式二 * @param productId 商品ID * @return 模式 ...

  2. FMDB实用攻略

    一.首先创建模型类 User.h #import <Foundation/Foundation.h> @interface User : NSObject @property(nonato ...

  3. SSL协议(HTTPS) 握手、工作流程详解(双向HTTPS流程)

    原文地址:http://www.cnblogs.com/jifeng/archive/2010/11/30/1891779.html SSL协议的工作流程: 服务器认证阶段:1)客户端向服务器发送一个 ...

  4. Java IO流体系中常用的流分类

    Java输入/输出流体系中常用的流分类(表内容来自java疯狂讲义) 注:下表中带下划线的是抽象类,不能创建对象.粗体部分是节点流,其他就是常用的处理流. 流分类 使用分类 字节输入流 字节输出流 字 ...

  5. Python文档

    详细的为代码编写文档,这其实是写好代码的重要部分. 常见编写代码的陷阱: 1.别忘了冒号.一定要记住在复合语句首行末未输入":" 2.从第一行开始.要确定顶层(无嵌套)程序代码从第 ...

  6. MyBatis复习

    一.对JDBC的总结 1.数据库连接,使用时就创建,不使用立即释放,对数据库进行频繁连接开启和关闭,造成数据库资源浪费,影响数据库性能. 解决方案:使用数据库连接池管理数据库连接. 2.将sql语句硬 ...

  7. A required class was missing while executing org.apache.maven.plugins:maven-war-plugin:2.1.1:war

    完美解决方案: http://stackoverflow.com/questions/18442753/a-required-class-was-missing-while-executing-org ...

  8. 【转载】H264--1--编码原理以及I帧B帧P帧

    ---------------------- 前言 ----------------------- H264是新一代的编码标准,以高压缩高质量和支持多种网络的流媒体传输著称,在编码方面,我理解的他的理 ...

  9. kettle转换提高性能拆分转换步骤_20161201

    今天是12月1号,前期用kettle做了月报自动报表的转换和作业,今天运行时候发现一个报表的程序跑起来失败,心里很纳闷,上过月刚跑的没问题,怎么会无缘无故的失败. 通过看kettle运行日志,发现一个 ...

  10. [纯小白学习OpenCV系列]官方例程01:Load and Display an Image

    Version: OpenCV 2.4.9 IDE    : VS2010 OS     : Windows --------------------------------------------- ...