LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
Example 1:
Input:
/ \ / \ [,,], [,,] Output: true
Example 2:
Input:
/ \ [,], [,null,] Output: false
Example 3:
Input:
/ \ / \ [,,], [,,] Output: false
方法一:使用递归(C++)
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p==NULL&&q==NULL)
return true;
if((!p&&q)||(p&&!q)||(p->val!=q->val))
return false;
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
}
LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++的更多相关文章
- same tree(判断两颗二叉树是否相等)
Input: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3] Output: true Example 2: Input: 1 1 / \ 2 2 [1,2], [1,nul ...
- LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ex ...
- [LeetCode]100. Same Tree判断树相同
dfs遍历一下判断 public boolean isSameTree(TreeNode p, TreeNode q) { if (p==null) { return q == null; } els ...
- (二叉树 递归 DFS) leetcode 100. Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- Leetcode 100 Same Tree 二叉树
就是判断两棵树的值和结构是否相同 注意:要判断是否所有的树节点是否为NULL /** * Definition for a binary tree node. * struct TreeNode { ...
- 剑指offer17:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
1 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 2 思路和方法 (1)先在A中找和B的根节点相同的结点 (2)找到之后遍历对应位置的其他结点, ...
- leetcode 100. Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- leetcode 100 Same Tree ----- java
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- Invert a binary tree 翻转一棵二叉树
Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4 / \ 2 7 / \ / \ 1 3 6 9翻转后: 4 / \ 7 ...
随机推荐
- 使用getInstance()方法的原因及作用
使用getInstance()方法的原因及作用 先举例说明: 下面是一个例子,为什么要把这个类实例化?有什么好处? //实例化 public static DBConnect instance; pu ...
- [Java Web学习]junit.framework.AssertionFailedError: No tests found in {Class}
No tests found in com.XXXXX.XXX.inboundPrepService.bizLogic.prepDeterminationEngine.workers.Determin ...
- 2019南昌邀请赛 L 计算几何 G(待补)
#include<bits/stdc++.h> const double PI=acos(-1.0); ; using namespace std; struct Point { doub ...
- explain和profiling分析查询SQL时间
mysql可以通过profiling命令查看到执行查询SQL消耗的时间. 默认情况下,mysql是关闭profiling的,命令: select @@profiling; +------------- ...
- java_集合类_简
Collection 来源于Java.util包,实用常用的数据结构,字面意思就是容器 主要方法 boolean add(Object o)添加对象到集合 boolean remove(Object ...
- Google - Largest Sum Submatrix
Given an NxN matrix of positive and negative integers, write code to find the submatrix with the lar ...
- Python 限制线程的最大数量(Semaphore)
import threadingimport time sem = threading.Semaphore(4) # 限制线程的最大数量为4个 def gothread(): with sem: # ...
- PythonStudy——枚举 enumerate
# 给可迭代器对象及迭代器对象添加迭代索引 s = 'abc' for v in enumerate(s): print(v) # (0 'a') | (1 'b') | (2 'c')
- 游戏编程模式 Game Programming Patterns (Robert Nystrom 著)
第1篇 概述 第1章 架构,性能和游戏 (已看) 第2篇 再探设计模式 第2章 命令模式 (已看) 第3章 享元模式 (已看) 第4章 观察者模式 (已看) 第5章 原型模式 (已看) 第6章 单例模 ...
- 创建一个dynamics 365 CRM online plugin (十一) - Handling Configuration data
Config data 可以在registering step 的时候来配置 配置好的config data 可以使用 constructor 来获取 Secure Config 和 UnSecure ...