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的更多相关文章

  1. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  2. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  3. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  4. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  5. 【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 ...

  6. 【leetcode刷题笔记】Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  7. 【leetcode刷题笔记】Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  8. 【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. 题解 ...

  9. 【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, ...

随机推荐

  1. C# 写日志到文件

    C# 写日志到文件 using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms ...

  2. delphi的字节对齐

    如果不改编译选项Delphi的Record默认也是4字节对齐的. 可以用编译开关指定 {$A4+}就是4字节对齐.同理{$A2+}.{$A1+}等.{$A1+}等同于Packed Record 主要容 ...

  3. 关于TCP/IP,这十个问题你都知道,就入门了!

    关于TCP/IP,必知必会的十个问题 本文整理了一些TCP/IP协议簇中需要必知必会的十大问题,既是面试高频问题,又是程序员必备基础素养.   TCP/IP十个问题 一.TCP/IP模型 TCP/IP ...

  4. Paxos 学习总结

    近期学习了分布式领域的重要算法Paxos,这里罗列下关键点当作总结.自己水平有限,难免存在谬误,恳请读者指正.本篇不包含Paxos的基本理论介绍.Paxos基础能够參考以下的学习资料章节. 1 Pax ...

  5. C#:异步编程和线程的使用(.NET 4.5 ),异步方法改为同步执行

    摘自:http://www.codeproject.com/Articles/996857/Asynchronous-programming-and-Threading-in-Csharp-N(葡萄城 ...

  6. Ubuntu安装CodeBlocks

    访问 https://launchpad.net/~damien-moore/+archive/ubuntu/codeblocks-stable,找到页面上加粗的那一段英文(以“ppa:”开头),如“ ...

  7. laravel模型建立和数据迁移和数据填充(数据填充没有成功)未完

    开始创建我们的第一个 Article 模型及其对应迁移文件了,我们在项目根目录运行如下 Artisan 命令一步到位: php artisan make:model Article -m -m 是 - ...

  8. R410自带SAS6IR卡折腾记

    因为需要一些做一些自动编译的工作,所以打算淘换一台多核的主机.淘宝找一圈,感觉换下来的dell R410 ~ R710 都可以. 综合对比了一下感觉最低配的R410就能满足要求,最后选择了:X5675 ...

  9. Spark高速上手之交互式分析

    1.1  Spark交互式分析 执行Spark脚本前,启动Hadoop的HDFS和YARN.Spark的shell提供 了简单方式去识别API.相同也有一个强大的工具去交互式地分析数据. 两种语言有这 ...

  10. EXTjs+SpringMVC+Mybatis实现照片的上传,下载,查看关键技术整理

    第一个问题:如何通过Extjs4实现照片上传的布局展示以及本地照片选择后的在一个区域内进行图片预览 实现照片上传的布局展示: items : [ { xtype : 'box', itemId : ' ...