[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 ...
随机推荐
- 配置 host only 后 nat不能上网了
如果只有nat 网关为nat 中设置的网关 eth0 启动第二块网卡host_only 网关就变成了 host_only中的网关 eth1 解决放法 route -n 看启用的是哪个网关 [roo ...
- 用T4模版生成对应数据库表的实体类
<#@ template debug="false" hostspecific="false" language="C#" #> ...
- LAMP的安装和注意事项
LAMP--Linux+Apache(httpd)+MySQL+PHP,是常用的web服务器架构,下面接受编译安装的过程,以及出现的错误. 注意事项: 1. 扩展epel源:参照:http://www ...
- SmallLocks
folly/SmallLocks.h This module is currently x64 only. This header defines two very small mutex types ...
- 实现一个最简单的plot函数调用:
实现一个最简单的plot函数调用: 1 import matplotlib.pyplot as plt 2 3 y=pp.DS.Transac_open # 设置y轴数据,以数组形式提供 4 5 x= ...
- Pthreads 信号量,路障,条件变量
▶ 使用信号量来进行线程间信息传递 ● 代码 #include <stdio.h> #include <pthread.h> #include <semaphore.h& ...
- jsp 学习 第1步 - 引入 jstl
通过 eclipse 新建 动态web项目 默认是没有引入 jstl, 则无法JSP页面引入相关标记. <%@ taglib prefix="c" uri="ht ...
- oracle执行sql文件
oracle执行sql文件 在PL/SQL中直接用command window执行就可以了: PL/SQL developer----->File------>New---->com ...
- 实战zabbix3.0.2 使用percona mysql插件监控mysql5.7
1.系统环境 [root@shard0 templates]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.2 ...
- Git----时光穿梭机01
看这篇文章之前可以先阅读 https://www.cnblogs.com/cxq0017/p/9645944.html 创建版本库这篇文章 我们已经成功地添加并提交了一个readme.txt文件,现 ...