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. VBA_Excel_教程:变量,数组

    Sub testVar() '变量 Dim strT1 As String strT1 = "A" '常量[加不加类型都可以] Const strT2 As String = &q ...

  2. flex polygon 序列化为txt 文本

    当我们要把一个地块导出为txt的时候,应该怎么写,这是比较有用的这样可以帮助我们存档之类的,这里是基于某个地方的独立坐标系,是基于自己发布地图,如果是用百度地图或者其他网上的地图可能不适用. pack ...

  3. IDE神器intellij idea的基本使用

    摘自: http://www.cnblogs.com/newpanderking/p/4887981.html 一.编码快捷键(比较常用的快捷键)该套快捷键选择的是:Mac OS X 10.5+ 1. ...

  4. java并发编程_CountDownLanch(倒计数锁存器)应用场景

    使用介绍: 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDown() 方法,所以在 ...

  5. KMeans的图像压缩

    # -*- coding: utf-8 -*- """ Created on Thu Aug 11 18:54:12 2016 @author: Administrato ...

  6. vi 使用

      1)命令 gf  ,可以从光标指定的文件位置打开对应文件 :bd回来 2)http://www.cnblogs.com/wangkangluo1/archive/2012/04/12/244495 ...

  7. 函数的定义和声明以及this

    this = $(this)[0]; var person = { name : "lisa", age : "20", init : function(){ ...

  8. 性能改善之For与Foreach

    关于For与Foreach的区别,博客园里已经有好多这样文章了,都分析的挺好:http://www.cnblogs.com/jobs/archive/2004/07/17/25218.aspx  不过 ...

  9. MongoDB中的连接池

    参见 http://www.cnblogs.com/huangfox/archive/2012/04/01/2428947.html

  10. spark textFile 困惑与解释

    在编写spark测试应用时, 会用到sc.textFile(path, partition) 当配置为spark分布式集群时,当你读取本地文件作为输入时, 需要将文件存放在每台work节点上. 这时会 ...