[leetcode] 11. 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)
{
return false;
} return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
就是这样。
[leetcode] 11. Same Tree的更多相关文章
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [LeetCode] 429. N-ary Tree Level Order Traversal_ Easy
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- LeetCode—— Invert Binary Tree
LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...
随机推荐
- emacs配置emacs-clang-complete-async
debian下需要安装apt安装下clang和llvm sudo apt-get install llvm-dev sudo apt-get install libclang-dev 网上抄来一个大神 ...
- C++ is-a关系
首先举一个例子: 在日常生活中,我们所说的眼镜大都是带框的眼镜,但是当提起隐形眼镜时,我们想一下它属不属于眼镜呢?答案肯定是属于的.这里的隐形眼镜和眼镜就是属于 is-a 的关系. 在面向对象编程过程 ...
- fatal error: mysql.h: No such file or directory
在ubuntu系统下安装mysql之后,和数据库连接的时候,出现如下错误:fatal error: mysql.h: No such file or directory 是因为缺少链接库,执行如下命名 ...
- hihocoder-1080题解
一.题目链接 http://hihocoder.com/problemset/problem/1080 二.题意 一维区间,需要做区间增加和区间置值,以及对整个区间求和. 三.思路 显然线段树是个利器 ...
- Quest.Central.for.DB2.v5.0.2.4下载地址
http://pan.baidu.com/s/1h5vgl 激活码 2-95710-02204-91891-68750yhason
- centos7上mysql无法启动也没有日志
报错的原因就是 [root@localhost duanxinli]# journalctl -xe-- Subject: Unit mysqld.service has begun start-up ...
- Readme.MD 例子
了解一个项目,恐怕首先都是通过其Readme文件了解信息.如果你以为Readme文件都是随便写写的那你就错了.github,oschina git gitcafe的代码托管平台上的项目的Readme. ...
- Redis 哨兵模式 带密码单机
语法 https://segmentfault.com/a/1190000002680804 启动3台redis 6379,6380,6381 cp 多个redis.conf文件 开启daemoniz ...
- BURG For Ubuntu 14.04 Trusty
sudo add-apt-repository ppa:n-muench/burg sudo apt-get updatesudo apt-get install burg burg-themes D ...
- Java匿名内部类的学习
新建一个抽象类或者接口,抽象类中只要有一个抽象方法就是抽象类,接口的定义是:里面的方法全部都是抽象方法,接口和抽象类不能直接实例化,需要子类来实现 /* 匿名内部类: 1.匿名内部类其实就是内部类的简 ...