public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null)
            return q == null;
        if (q == null)
            return p == null;

        TreeIterator ip = new TreeIterator(p);
        TreeIterator iq = new TreeIterator(q);

        while (ip.hasNext() && iq.hasNext()) {
            if (!equalNode(ip.next(), iq.next()))
                return false;
        }

        if (ip.hasNext() || iq.hasNext())
            return false;

        return true;
    }