/** * 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 list1 = new List (); List list2 = new List (); void postTree(TreeNode tree, int type) { if (type == 1) { 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, 1); postTree(q, 2); var len1 = list1.Count; var len2 = list2.Count; if (len1 != len2) { return false; } else { for (int i = 0; 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; } }}