【LeetCode】100 - 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.
Solution 1:recursion,the key is to find out all situations of return true and false
/**
* Definition for a binary tree node.
* 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 && !q)return true;
if(p && !q)return false;
if(!p && q)return false;
if(isSameTree(p->left, q->left) && isSameTree(p->right, q->right) && p->val==q->val)return true;
return false; }
};
Solution 2: 非递归,待续
【LeetCode】100 - Same Tree的更多相关文章
- 【LeetCode】100. Same Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- 【LeetCode】100. Same Tree (2 solutions)
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【一天一道LeetCode】#100. Same Tree(100题大关)
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...
- 【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
- 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
随机推荐
- Linux系统文件权限&目录权限
linux系统一切都是文件,文件和目录的所属与权限--来分别规定所有者.所有组.其余人的读.写.执行权限. 读(read),写(write),执行(excute)简写为(r.w.x),也可以以用(4. ...
- powerdesigner jdbc 连接 oracle
实验环境: powerdesigner 15 oracle 11g jdk1.6.0_43 提示:jdk必须选择32位,64位会报 "Could not Initialize JavaVM ...
- Partitioner
partitioner 是map中的数据映射到不同的reduce时的根据.一般情况下,partitioner会根据数据的key来把数据平均分配给不同的reduce,同时保证相同的key分发到同一个re ...
- Android NDK 和 OpenCV 整合开发总结(3)
Android NDK 和 OpenCV 整合开发总结(3) http://hujiaweibujidao.github.io/blog/2013/11/18/android-ndk-and-open ...
- c# 任意多个数,求最大值
c# 任意多个数,求最大值 使用parms: 正在研究中,如果有好的方案,可评论,共同进步,共同提高,谢谢!
- flex 4 写皮肤
皮肤容器:s:SparkSkin 主机组件: [HostComponent("spark.components.Panel")] 绘制: <s:Group left=&qu ...
- 超大整数运算算法——为RSA加密算法提供运算工具
/* program: Large integer operations * Made by: Daiyyr * date: 2013/07/09 * This software is licen ...
- python 捕获 shell/bash 脚本的输出结果
#!/usr/bin/python## get subprocess module import subprocess ## call date command ##p = subprocess.Po ...
- 【转】在eclipse上使用Git
在eclipse上安装好git插件后,进行操作 安装好后,进行,一个简单配置,填入我们的用户名和邮箱 >>Preferences>Team>Git>Configurati ...
- Android 用户界面---拖放(Drag and Drop)(三)
设计拖放操作 本节主要内容如下: 1. 如何开始拖拽: 2. 在拖拽期间如何响应事件: 3. 如何响应落下事件: 4. 如何结束拖放操作. 开始拖拽 用户使用一个拖拽手势开始拖拽,通常是在 ...