LeetCode题解之 Subtree of Another Tree
1、题目描述
2、问题分析
判断一个节点,然后判断子树。
3、代码
bool isSubtree(TreeNode* s, TreeNode* t) {
if (s == NULL)
return false;
else {
return isSame(s,t) || isSubtree(s->left, t) || isSubtree(s->right, t);
} } bool isSame(TreeNode *t1, TreeNode *t2)
{
if (t1 == NULL && t2 == NULL) return true;
if (t1 == NULL || t2 == NULL) return false; return (t1->val == t2->val) && isSame(t1->left, t2->left) && isSame(t1->right , t2->right);
}
LeetCode题解之 Subtree of Another Tree的更多相关文章
- LeetCode算法题-Subtree of Another Tree(Java实现)
这是悦乐书的第265次更新,第278篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第132题(顺位题号是572).给定两个非空的二进制树s和t,检查树t是否具有完全相同的 ...
- LeetCode题解:(114) Flatten Binary Tree to Linked List
题目说明 Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 ...
- [LeetCode 题解]: Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 【leetcode】572. Subtree of Another Tree
题目如下: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...
- [LeetCode 题解]: Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- LeetCode题解之Find Bottom Left Tree Value
1.题目描述 2.问题分析 使用层序遍历思想 3.代码 int findBottomLeftValue(TreeNode* root) { if (root == NULL) ; queue<T ...
- LeetCode题解之Diameter of Binary Tree
1.题目描述 2.分析 深度优先. 3.代码 int ans; int diameterOfBinaryTree(TreeNode* root) { ans = ; depth(root); ; } ...
- LeetCode题解之 Increasing Order Search Tree
1.题目描述 2/问题分析 利用中序遍历,然后重新构造树. 3.代码 TreeNode* increasingBST(TreeNode* root) { if (root == NULL) retur ...
- [LeetCode 题解]: Binary Tree Preorder Traversal
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
随机推荐
- 关于如何使`(a === 1 && a === 2 && a === 3)`返回`true`问题的思考
看见这个面试题目,第一反应就是在变量a取值时进行了一些改变,那就要用getter,关于存取器的介绍可以看这里 var temp = 1; Object.defineProperty(window, ' ...
- Servlet-session简介及使用场景
- python numpy安装
一.python下的numpy安装方法 第一步:安装python,这里不做介绍. 第二步:打开cmd看python是否安装成功. 第三步:输入 python -m pip install -U pip ...
- Tensflow的equal函数
一.函数 tf.equal() equal( x, y, name=None ) tf.equal(A, B)是对比这两个矩阵或者向量的相等的元素,如果是相等的那就返回True,反正返回False,返 ...
- Python:SQLMap源码精读—start函数
源代码 def start(): """ This function calls a function that performs checks on both URL ...
- Android中为什么需要服务?
在解释这个问题之前, 先来看一个Android系统中进程的优先级(从高到低) 前台进程(foreground process ): 一个应用程序启动, 并且可以直接相应用户的点击,触摸事件.那么这样 ...
- go 通过http发送图片file内容
package main import ( "encoding/json" "fmt" "io/ioutil" "net/http ...
- Jenkins之使用Pyinstaller构建Python应用程序
目录 1. 极简概述 2. Jenkins配置 2.1 安装JDK 2.2 安装Jenkins 3. 安装Docker 4. 使用PyInstaller构建Python应用程序 4.1 Fork 一个 ...
- 分布式系统CAP理论以及注册中心选择
CAP定理:指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者不可同时获得. 一致性(C-数据 ...
- 和我一起打造个简单搜索之SpringDataElasticSearch关键词高亮
前面几篇文章详细讲解了 ElasticSearch 的搭建以及使用 SpringDataElasticSearch 来完成搜索查询,但是搜索一般都会有搜索关键字高亮的功能,今天我们把它给加上. 系列文 ...