关于镜像树的相关操作,利用递归可以很简单的解决问题。
注意判断根节点是不是null
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isSymmetric(TreeNode root) { if(root == null) { return true; } return dfs(root.left, root.right); } public boolean dfs(TreeNode left, TreeNode right) { if(left==null && right==null) { return true; } if((left!=null&&right==null) || (left==null&&right!=null) || left.val!=right.val) { return false; } return dfs(left.left, right.right) && dfs(left.right, right.left); } }
原文:http://www.cnblogs.com/wxisme/p/4456763.html