所谓二叉树的镜像树就是把一个节点下的左右孩子交换,操作给定的二叉树,将其变换为源二叉树的镜像。
观察图像可知,全部左右节点交换即可,思路比较简单。
/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public void Mirror(TreeNode root) { if(root != null){ TreeNode temp = root.left; root.left = root.right; root.right = temp; }else{ return; } Mirror(root.left); Mirror(root.right); } }
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2018 群英 版权所有 茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号-36 粤公网安备 44090202000006号 粤工商备P091701000595