二叉树的对称和镜像问题
对称的二叉树
http://www.nowcoder.com/questionTerminal/ff05d44dfdb04e1d83bdbdab320efbcb
这道题其实就是验证二叉树是否为镜像二叉树
一层一层递归判断:
public class Solution { boolean isSymmetrical(TreeNode pRoot) { return pRoot == null || judge(pRoot.left, pRoot.right); } public boolean judge(TreeNode left, TreeNode right){ if(left == null && right == null) return true; else if(left == null || right == null) return false; if(left.val != right.val) return false; else return judge(left.left, right.right) && judge(left.right, right.left); } }
public class Solution { public void Mirror(TreeNode root) { if(root == null) return ; TreeNode node = root.left; root.left = root.right; root.right = node; Mirror(root.left); Mirror(root.right); } }