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否是空.有的 ...
随机推荐
- 【JVM】jvm至jstack命令
一.介绍 jstack是java虚拟机自带的一种堆栈跟踪工具.jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项&qu ...
- ARP命令详解
ARP是一个重要的TCP/IP协议,并且用于确定对应IP地址的网卡物理地址.实用arp命令,我们能够查看本地计算机或另一台计算机的ARP高速缓存中的当前内容.此外,使用arp命令,也可以用人工方式输入 ...
- Oracle GI 日志收集工具 - TFA 简介
转载自:https://blogs.oracle.com/Database4CN/entry/tfa_collector_%E4%BB%8B%E7%BB%8D 1.TFA的目的: TFA是个11.2版 ...
- 高大上的JS工具
EtherPad (协同文件编辑): EtherCalc (协同电子表格编辑)
- OSI7层封包解包动态图-数据在网络中的传输过程.gif
- google code 或 git 免用户名和密码 .netrc 在windows中的操作 _netrc
1.首先用不包含用户名URL CLONE “git clone https://code.google.com/p/YourProjName/” .而不能用 “git clone https://Yo ...
- svn 提交代码 自动过滤技巧,自动过滤不想提交的文件和文件夹
原文:http://www.cnblogs.com/codealone/p/3208620.html 打开SVN客户端-----常规设置------全局忽略样式,添加 bin obj debug Re ...
- AsyncTask使用详细说明
AsyncTask使用: 在开发Android应用时必须遵守单线程模型的原则: Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行.在单线程模型中始终要记住两条法则: 1. 不要 ...
- wkhtmltopdf+itext实现html生成pdf文件的打印下载(适用于linux及windows)
目中遇到个根据html转Java的功能,在java中我们itext可以快速的实现pdf打印下载的功能,在itext中我们一般有以下三中方式实现 配置pdf模板,通过Adobe Acrobat 来设置域 ...
- JavaScript之图片操作3
在页面布局中,常常会用到九宫格布局,如下图所示: 本次我们就以九宫格为基础进行图片的布局操作,首先我们以上面的图片的为例,假设每个格子的大小都相同,将每一个格子相对其父元素进行定位,这样,我们只需要控 ...