/**
* 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