Symmetric Tree

Symmetric Tree

问题:

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

思路:

  dfs

我的代码:

public class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null)    return true;
        return helper(root.left,root.right);
    }
    public boolean helper(TreeNode left, TreeNode right)
    {
        if(left == null && right == null)   return true;
        if(left == null || right == null)   return false;
        return left.val == right.val && helper(left.left, right.right) && helper(left.right, right.left);
    }
}
View Code

 

posted on 2015-03-07 19:39  zhouzhou0615  阅读(107)  评论(0编辑  收藏  举报

导航