二叉树的镜像 发表于 2019-03-18 | 分类于 剑指offer | | 阅读次数: 字数统计: 142字 | 阅读时长 ≈ 1分钟 题目描述操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述 解题思路 采用递归解决此题即可,不断交换根节点下的左右结点,再以左右结点分别为根节点做交换。 代码123456789101112131415161718192021222324252627282930/**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){ return; } exchange(root); Mirror(root.left); Mirror(root.right); } private TreeNode exchange(TreeNode root){ TreeNode a = root.left; root.left = root.right; root.right = a; return root; }} 坚持原创技术分享,您的支持将鼓励我继续创作! 打赏 微信支付 支付宝
v1.5.2