码迷,mamicode.com
首页 > 其他好文 > 详细

Symmetric Tree

时间:2015-03-07 19:58:45      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Symmetric Tree

标签:

原文地址:http://www.cnblogs.com/sunshisonghit/p/4320815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!