[剑指offer]二叉树的镜像
Posted moonbeautiful
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[剑指offer]二叉树的镜像相关的知识,希望对你有一定的参考价值。
题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:
二叉树的镜像定义:源二叉树 8 / 6 10 / \ / 5 7 9 11 镜像二叉树 8 / 10 6 / \ / 11 9 7 5
题目链接:
https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
package com.sunshine.OFFER66_SECOND; public class TreeNode int val; TreeNode left; TreeNode right; TreeNode(int x) val = x;
package com.sunshine.OFFER66_SECOND; import org.junit.Test; public class A18_Mirror @Test public void test() TreeNode n1 =new TreeNode(1); TreeNode n2 =new TreeNode(2); TreeNode n3 =new TreeNode(3); TreeNode n4 =new TreeNode(4); TreeNode n5 =new TreeNode(5); TreeNode n6 =new TreeNode(6); TreeNode n7 =new TreeNode(7); n1.left=n2; n1.right=n3; n2.left=n4; n2.right=n5; n3.left=n6; n3.right=n7; Mirror(n1); TreeUtility.printTreeOfLine(n1); public void Mirror(TreeNode root) if(root == null ) return; TreeNode right = root.right; root.right = root.left; root.left = right; Mirror(root.left); Mirror(root.right);
以上是关于[剑指offer]二叉树的镜像的主要内容,如果未能解决你的问题,请参考以下文章
剑指 Offer 26. 树的子结构 / 剑指 Offer 27. 二叉树的镜像 / 剑指 Offer 28. 对称的二叉树 / 剑指 Offer 29. 顺时针打印矩阵