/**
* 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. CountDownLatch的简单理解

    CountDownLatch的概念 CountDownLatch是一个同步工具类,用来协调多个线程之间的同步,或者说起到线程之间的通信(而不是用作互斥的作用). CountDownLatch能够使一个 ...

  2. STS或eclipse安装SVN插件(转)

    安装sts--SVN插件 简介:sts是与eclipse类似的Java IDE开发工具(不了解的百度) 1.sts菜单栏 help->install New Software 依据大家的版本选择 ...

  3. V4L2驱动内核文档翻译(一)

    随着一些视频或者图像硬件的复杂化,V4L2驱动也越来越趋于复杂.许多硬件有多个IC,在/dev下生成多个video设备或者其他的诸如,DVB,ALSA,FB,I2C ,IR等等非V4L2的设备.所以, ...

  4. xsl -fo 了解

    XSL-FO是用于格式化XML数据的语言,全称为Extensible Stylesheet Language Formatting Objects(格式化对象的可扩展样式表语言),是W3C参考标准,现 ...

  5. php 中的引用

    php 有类似 C 中的指针 &. 但在 php 中叫 引用. 虽然和 传地址很像,但是差别很大.(估计底层实现应该差不多,只是猜想,有机会再研究) 这里有一个关于 php 的对象的赋值其实就 ...

  6. 打印数组所有排列 python

    本人.net一名,最近在看数据结构与算法分析,中间涉及的一些比较有意思的算法题,打算用python实现以下.选择python的原因,就是想熟悉一下python的语法,和pycharm基本的应用. 本篇 ...

  7. graphEdit

    目的 实现支持算法流程图 参考代码 c# DSGraphEdit - CodeProject NetworkView: A WPF custom control for visualizing and ...

  8. shell脚本 如何调用Mysql的存储过程 解决方案

    今天遇到一个在shell脚本里面要调用MySQL的存储过程,查阅了很多资料,发现有的都不好用,自己调试出了如下一种,拿来共享: 用mysql -e “ ”: 例如:   mysql -uroot -p ...

  9. js实现动态球形标签云

    HTML 原文演示地址:http://www.17sucai.com/pins/demoshow/8108 <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...

  10. BASIC-14_蓝桥杯_时间转换

    示例代码: #include <stdio.h> int main(void){ int t = 0 , h = 0 , m = 0 , s = 0 ; scanf("%d&qu ...