剑指Offer-二叉树的镜像

题目描述#

操作给定的二叉树,将其变换为源二叉树的镜像。

输入描述:

Copy
二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5

思路#

先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子节点,当交换完所有的非叶子结点的左右子结点之后,就得到了树的镜像。

思路一:

递归

思路二:

借助栈遍历二叉树

代码实现#

Copy
package Tree; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; /** * 二叉树的镜像 * 操作给定的二叉树,将其变换为源二叉树的镜像。 * 二叉树的镜像定义:源二叉树 * 8 * / \ * 6 10 * / \ / \ * 5 7 9 11 * 镜像二叉树 * 8 * / \ * 10 6 * / \ / \ * 11 9 7 5 * 思路: * 先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子节点, * 当交换完所有的非叶子结点的左右子结点之后,就得到了树的镜像 */ public class Solution15 { public static void main(String[] args) { Solution15 solution15 = new Solution15(); int[] array = {8, 6, 10, 5, 7, 9, 11}; TreeNode treeNode = solution15.createBinaryTreeByArray(array, 0); System.out.println(treeNode.left.right.val); solution15.Mirror_2(treeNode); System.out.println(treeNode.left.right.val); } /** * 用栈遍历二叉树 * * @param root */ public void Mirror_2(TreeNode root) { if (root == null) { return; } Deque<TreeNode> deque = new ArrayDeque<>(); deque.push(root); while (!deque.isEmpty()) { TreeNode node = deque.pop(); if (node.left != null || node.right != null) { TreeNode temp = node.left; node.left = node.right; node.right = temp; } if (node.left != null) { deque.push(node.left); } if (node.right != null) { deque.push(node.right); } } } /** * 递归 * * @param root */ public void Mirror(TreeNode root) { if (root == null) { return; } if (root.left == null && root.right == null) { return; } TreeNode temp = root.left; root.left = root.right; root.right = temp; if (root.left != null) { Mirror(root.left); } if (root.right != null) { Mirror(root.right); } } public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } public TreeNode createBinaryTreeByArray(int[] array, int index) { TreeNode tn = null; if (index < array.length) { int value = array[index]; tn = new TreeNode(value); tn.left = createBinaryTreeByArray(array, 2 * index + 1); tn.right = createBinaryTreeByArray(array, 2 * index + 2); return tn; } return tn; } }
posted @   武培轩  阅读(299)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示
CONTENTS