[leetcode] 100. 相同的树

100. 相同的树

判断两颗二叉树是否完全相同,把该树转变为数组展示形式,
前序遍历,然后看结果是否一致即可

class Solution {
    String s = "";

    void dfs(TreeNode tree){
        if (tree == null) return;
        s+=tree.val;
        if (tree.left!=null) dfs(tree.left);
        if (tree.right!=null) dfs(tree.right);
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        String a = "";
        dfs(p);
        a = s;
        s = "";
        dfs(q);
        return a.equals(s);
    }
}
posted @ 2018-11-04 22:05 ACBingo 阅读(...) 评论(...) 编辑 收藏