第一种方法就是先序遍历的递归形式,第二种方法是层次遍历的非递归算法
class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) return true; if (p == null || q == null) return false; if(p.val!=q.val)return false; return isSameTree(p.left, q.left)&&isSameTree(p.right, q.right); } }
层次遍历的非递归算法
...