leetcode100
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
List<TreeNode> list1 = new List<TreeNode>();
List<TreeNode> list2 = new List<TreeNode>(); void postTree(TreeNode tree, int type)
{
if (type == )
{
list1.Add(tree);
}
else
{
list2.Add(tree);
}
if (tree != null)
{
if (tree.left != null)
{
postTree(tree.left, type);
}
else
{
postTree(null, type);
} if (tree.right != null)
{
postTree(tree.right, type);
}
else
{
postTree(null, type);
}
} } public bool IsSameTree(TreeNode p, TreeNode q)
{
postTree(p, );
postTree(q, ); var len1 = list1.Count;
var len2 = list2.Count; if (len1 != len2)
{
return false;
}
else
{
for (int i = ; i < len1; i++)
{
if (list1[i] == null && list2[i] != null)
{
return false;
}
if (list1[i] != null && list2[i] == null)
{
return false;
}
if (list1[i] != null && list2[i] != null && list1[i].val != list2[i].val)
{
return false;
}
}
return true;
}
}
}
https://leetcode.com/problems/same-tree/#/description
leetcode100的更多相关文章
- LeetCode100:Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [Swift]LeetCode100. 相同的树 | Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- LeetCode100.相同的树
给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1 ...
- 【leetcode-100】 简单 树相关题目
100. 相同的树 (1过,熟练) 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 ...
- leetcode-100. Same Tree · Tree + DFS + Queue
题面 对比两棵二叉树是否相同,返回结果. 思路 1. 递归解决DFS 首先判断根节点,如果都空,返回true: 如果一空一不空,返回false: 如果都不空,判断两节点值是否相同,若不同,返回fals ...
- LeetCode572. 另一个树的子树
题目 本题目一开始想要通过二叉树遍历KMP匹配,但看来实现比较复杂 不如直接暴力匹配,本题和LeetCode100.相同的树有共通之处 1 class Solution { 2 public: 3 b ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
随机推荐
- hdu2088
hdu2088 #include<stdio.h> #include<algorithm> using namespace std; ]; int main(){ ; ){ , ...
- Immutable集合
转自:https://blog.csdn.net/michaellufhl/article/details/6314333 大家都知道JDK提供了Collections.UnmodifiableLis ...
- 修改ThinkPHP缓存为Memcache的方法
一般来说,ThinkPHP的默认缓存方式是以File文件方式实现的,运行时会在/Runtime/Temp 下生成很多的缓存文件. 有的情况下服务器装了memcached之后,需要将ThinkPHP的缓 ...
- How Distributed Outer Joins on PostgreSQL with Citus Work
转自: https://docs.citusdata.com/en/v7.5/articles/outer_joins.html SQL is a very powerful language for ...
- JUC集合之 ConcurrentLinkedQueue
ConcurrentLinkedQueue介绍 ConcurrentLinkedQueue是线程安全的队列,它适用于"高并发"的场景. 它是一个基于链接节点的无界线程安全队列,按照 ...
- EF Code First Migrations数据库迁移 (转帖)
1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...
- jQuery与DOM对象的转换
一.jQuery与DOM对象的转换. 1.jQuery对象转换为DOM对象:$cr[0] 或 $cr.get(0) $cr为jQuery对象 2.DOM对象转换为jQuery对象:$(cr) cr为D ...
- ML(3.1): NavieBayes R_e1071
朴素贝叶斯方法是一种使用先验概率去计算后验概率的方法, 具体见ML(3): 贝叶斯方法 R包 ① e1071::e1071 ② klaR::klaR 参考资料:https://en.wikibooks ...
- Unigui的Grid添加汇总栏
- BASIC-15_蓝桥杯_字符串对比
示例代码: #include <stdio.h>#define N 10 int main(void){ int i = 0 ; int len1 = 0 , len2 = 0 , fla ...