【6_100】Same Tree
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.
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的更多相关文章
- 【BZOJ】2631: tree LCT
[题意]给定n个点的树,每个点初始权值为1,m次操作:1.x到y的点加值,2.断一条边并连一条边,保证仍是树,3.x到y的点乘值,4.x到y的点权值和取模.n,m<=10^5. [算法]Link ...
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- 【题解】Digit Tree
[题解]Digit Tree CodeForces - 716E 呵呵以为是数据结构题然后是淀粉质还行... 题目就是给你一颗有边权的树,问你有多少路径,把路径上的数字顺次写出来,是\(m\)的倍数. ...
- 【题解】[P4178 Tree]
[题解]P4178 Tree 一道点分治模板好题 不知道是不是我见到的题目太少了,为什么这种题目都是暴力开值域的桶QAQ?? 问点对,考虑点分治吧.直接用值域树状数组开下来,统计的时候直接往树状数组里 ...
- 【总结】Link-Cut Tree
这是一篇关于LCT的总结 加删边的好朋友--Link Cut Tree Link-Cut Tree,LCT的全称 可以说是从树剖引出的问题 树剖可以解决静态的修改或查询树的链上信息:那如果图会不断改变 ...
- 【BZOJ】1468: Tree(POJ1741) 点分治
[题意]给定带边权树,求两点距离<=k的点对数.n<=40000. [算法]点分治 [题解]对于一个区域,选择其重心x作为根,则划分出来的每棵子树都是子区域,可以证明至多划分log n次( ...
- 【题解】【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 ...
- 【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 ...
- 【leetcode】Symmetric Tree
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
随机推荐
- c# 串口编程
http://news.ccidnet.com/art/32859/20100524/2067861_4.html 字节缓冲器处理类: /// <summary> /// 字节缓冲器 // ...
- cs11_c++_lab2
Matrix.hh class Matrix { int row; int col; int *p; public: Matrix(); Matrix(int x,int y); ~Matrix(); ...
- Hadoop HDFS编程 API入门系列之从本地上传文件到HDFS(一)
不多说,直接上代码. 代码 package zhouls.bigdata.myWholeHadoop.HDFS.hdfs5; import java.io.IOException; import ja ...
- Kafka深入理解-2:Kafka的Log存储解析
摘自http://blog.csdn.net/jewes/article/details/42970799 引言 Kafka中的Message是以topic为基本单位组织的,不同的topic之间是相互 ...
- socket笔记
参考: http://www.cnblogs.com/dolphinX/p/3460545.html http://www.cnblogs.com/wei2yi/archive/2011/03/23/ ...
- Linux驱动框架之framebuffer驱动框架
1.什么是framebuffer? (1)framebuffer帧缓冲(一屏幕数据)(简称fb)是linux内核中虚拟出的一个设备,framebuffer向应用层提供一个统一标准接口的显示设备.帧缓冲 ...
- 使用CocoaPods配置工程
1.首先搭建环境,配置CocoaPods,具体请参考 http://code4app.com/article/cocoapods-install-usage 2.打开终端,输入 cd 空格 把工程拖入 ...
- JQuery中$(document)是什么意思有什么作用
$(document).ready(fn):当DOM载入就绪可以查询及操纵时绑定一个要执行的函数,因为它可以极大地提高web应用程序的响应速度 首先我解释一下jQuery jQuery有一个用来作为D ...
- jdk1.6 反射性能对比
ReflectPerformance.java package aaa.bbb.ccc; import java.lang.reflect.Method; public class ReflectPe ...
- Linux系统编程-防止僵尸进程产生的常用方法
1.父进程调用wait函数或waitpid函数回收子进程. 2.让init进程去处理子进程回收工作,代码中加上"signal(SIGCHLD, SIG_IGN)"这句话.