/**
* 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的更多相关文章

  1. LeetCode100:Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  2. [Swift]LeetCode100. 相同的树 | Same Tree

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  3. LeetCode100.相同的树

    给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1 ...

  4. 【leetcode-100】 简单 树相关题目

    100. 相同的树 (1过,熟练) 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 ...

  5. leetcode-100. Same Tree · Tree + DFS + Queue

    题面 对比两棵二叉树是否相同,返回结果. 思路 1. 递归解决DFS 首先判断根节点,如果都空,返回true: 如果一空一不空,返回false: 如果都不空,判断两节点值是否相同,若不同,返回fals ...

  6. LeetCode572. 另一个树的子树

    题目 本题目一开始想要通过二叉树遍历KMP匹配,但看来实现比较复杂 不如直接暴力匹配,本题和LeetCode100.相同的树有共通之处 1 class Solution { 2 public: 3 b ...

  7. leetcode_二叉树篇_python

    主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...

随机推荐

  1. freemarker逻辑判断写法#if

    <li class="<#if (position.flag)! =='haha1'>hide<#else >show</#if>"> ...

  2. 转 Katana 项目入门

    Katana 项目入门 Howard Dierking 当 ASP.NET 首次在 2002 年发布时,时代有所不同. 那时,Internet 仍处于起步阶段,大约有 5.69 亿用户,每个用户平均每 ...

  3. C语言面试题3

    编程题 1.读文件file1.txt的内容(例如): 123456 输出到file2.txt: 563412 #include <stdio.h> #include <stdlib. ...

  4. Word中回车和网页换行替换

    回车^p 换行^l 用编辑中的查找替换即可 查找^l,替换为^p (一)有建议说按Delete键一个一个将其删除,这是麻烦的,其实可用WORD的查找替换工具一次性替换成回车符.查找--点特殊字符--手 ...

  5. linux 异步信号的同步处理方式

    关于代码的可重入性,设计开发人员一般只考虑到线程安全,异步信号处理函数的安全却往往被忽略.本文首先介绍如何编写安全的异步信号处理函数:然后举例说明在多线程应用中如何构建模型让异步信号在指定的线程中以同 ...

  6. js实现loading简单的遮套层

    弹出个div  设置div的背景色及透明度当加载完成后remove这个div  或者 隐藏至于淡入淡出通过setTimeout 或者setInterval改变透明度试试 .test{     widt ...

  7. [模板] Miller_Rabin素数判断代码实现存档

    就是....存存代码吧. Miller_Rabin的最核心部分在于二次探测定理和费马小定理.后者在同余/逆元的题目里面或多或少都有提及吧.....前者也很简单. 总而言之,Miller_Rabin不算 ...

  8. java程序怎么在一个电脑上只启动一次,只开一个进程

    目录 <linux文件锁flock> <NIO文件锁FileLock> <java程序怎么在一个电脑上只启动一次,只开一个进程> 方案1: 单进程程序可以用端口绑定 ...

  9. [转]IIS 允许/禁止 目录浏览

    <?xml version="1.0" encoding="utf-8"?> <configuration> <system.we ...

  10. mysql主从复制——双机互为主从

    第一.mysql主从复制(一主一从)怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下:首先需要做一些清理工作,如果之前配置了主从,但是配置失败了.结果会在/var/lib/mysql/ ...