[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 ...
随机推荐
- File根据inputstream复制文件到临时目录,使用完之后删除
项目中有这个需求: 1)上传文件通过公司平台的校验,校验成功后,通过接口,返回文件流: 2)我们根据这个文件流进行操作.这里,先将文件流复制文件到项目临时目录WEB-INF/temp;文件使用完毕,删 ...
- ThinkJava-File类
1.1目录列表器: package com.java.io; import java.io.File; import java.io.FilenameFilter; import java.util. ...
- Java-Runoob-高级教程-实例-字符串:01. Java 实例 – 字符串比较
ylbtech-Java-Runoob-高级教程-实例-字符串:01. Java 实例 – 字符串比较 1.返回顶部 1. Java 实例 - 字符串比较 Java 实例 以下实例中我们通过字符串函 ...
- JQuery input file 上传图片
表单元素file设置隐藏,通过其他元素打开: .imgfile为input file $(".ul").click(function () {return $(".img ...
- 【UVALive】4094 WonderTeam(神结论)
题目 传送门:QWQ 分析 好神的结论啊 看代码吧(Length只有85) 代码 顺手压了压代码 目前代码长度rk1 vjudge #include <iostream> ?:n ...
- 「小程序JAVA实战」小程序开源搜索组件(53)
转自:https://idig8.com/2018/09/22/xiaochengxujavashizhanxiaochengxukaiyuansousuozujian52/ 上次说了可以在视频中通过 ...
- 牛X的FieldBlur
[牛X的FieldBlur] Use Field Blur to build a gradient of blurs, by defining multiple blur points with di ...
- uva10288 Coupons 【概率 分数】
题目: 题意: 一共n种不同的礼券,每次得到每种礼券的概率相同.求期望多少次可以得到所有n种礼券.结果以带分数形式输出.1<= n <=33. 思路: 假设当前已经得到k种,获得新的一种的 ...
- 使用javascript,jquery实现的图片轮播功能
使用javascript,jquery实现的图片轮播功能本功能采用最基础的javascript和一些简单的jquery技术实现,易理解,以修改使用,代码简易,适合刚开始接触到网站开发的朋友们参考.可以 ...
- iOS下nil 、NULL、 Nil 、NSNull的区别
1.nil,定义一个空的实例,指向OC中对象的空指针. 示例代码: NSString *someString = nil; NSURL *someURL = nil; id someObject = ...