【leetcode刷题笔记】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.
解题:还是简单的递归,把情况考虑清楚就可以了:
根节点都为空,树相同;
根节点一方为空,另一方不为空,树不相同;
根节点值不想等,树不相同;
否则,递归比较左右子树是否相同。
代码:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if(p == NULL && q == NULL)
return true;
if((p == NULL && q != NULL) ||(p != NULL && q == NULL))
return false;
if(p->val != q->val)
return false;
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
};
Java版本代码:
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
int ispempty = p == null?0:1;
int isqempty = q == null?0:1;
if(ispempty != isqempty)
return false;
return isSameTreeHelper(p, q);
}
public boolean isSameTreeHelper(TreeNode p,TreeNode q){
if(p == null && q == null)
return true;
if(p.val != q.val)
return false;
int pleftempty = p.left == null?0:1;
int qleftempty = q.left == null?0:1;
if(pleftempty != qleftempty)
return false;
if(isSameTreeHelper(p.left, q.left) == false)
return false;
int prightempty = p.right == null?0:1;
int qrightempty = q.right == null?0:1;
if(prightempty != qrightempty)
return false;
if(isSameTreeHelper(p.right, q.right) == false)
return false;
return true;
}
}
Java版本写复杂了=。=
【leetcode刷题笔记】Same Tree的更多相关文章
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- 【leetcode刷题笔记】Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 【leetcode刷题笔记】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【leetcode刷题笔记】Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解 ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
随机推荐
- CentOs yum源安装 nginx
1 更新源 [root@server ~]#rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.no ...
- 堆排序算法的java实现
堆积排序(Heapsort)是指利用堆积树(堆)这种数据结构所设计的一种排序算法,可以利用数组的特点快速定位指定索引的元素.堆排序是不稳定的排序方法,辅助空间为O(1), 最坏时间复杂度为O ...
- 关于inittab的几个命令
1. 查看default runlevel(默认运行等级)的方法: $cat /etc/inittab | grep id id:3:initdefault: # <id>:<run ...
- c++学习笔记4,派生类的构造函数与析构函数的调用顺序(一)
測试源代码: //測试派生类的构造函数的调用顺序何时调用 //Fedora20 gcc version=4.8.2 #include <iostream> using namespace ...
- RecyclerView 踩坑
一.RecyclerView设置拖动后怎么监听拖动的开始和结束 ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.Cal ...
- Office365client通过本地方式批量部署(即点即用部署)
当企业用户拥有Office 365 ProPlus的许可后,可登陆Office 365.自行下载Officeclient安装部署 以上仅仅是理想情况,实际情况是企业用户较多,IT水平參差不齐,企业的带 ...
- java工程中当前目录在html中的设置
本地启动server的时候总是去读"/"的, 但到了服务器上,如果当前目录是服务器根目录下的一个文件夹,就应该设: <head> <meta charset=&q ...
- TensorFlow CNN 測试CIFAR-10数据集
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50738311 1 CIFAR-10 数 ...
- willMoveToParentViewController和didMoveToParentViewController
本文转载至 http://blog.csdn.net/yongyinmg/article/details/40619727 iOS 5.0 后UIViewController新增:willMoveTo ...
- python 基础 9.10 删除数据
#/usr/bin/python #-*- coding:utf-8 -*- #@Time :2017/11/24 4:40 #@Auther :liuzhenchuan #@File : ...