标签:post integer length treenode string import tor mirror 镜像
1 package offer; 2 3 import Struct.TreeNode; 4 5 /** 6 * 7 * 二叉树的镜像定义:源二叉树 8 8 9 / 10 6 10 11 / \ / 12 5 7 9 11 13 镜像二叉树 14 8 15 / 16 10 6 17 / \ / 18 11 9 7 5 19 * @author 爱不会绝迹 20 * 21 */ 22 public class Problem08 { 23 /** 24 * 25 * 镜像二叉树 26 * @param root 27 */ 28 public static void Mirror(TreeNode root) { 29 if(root!=null){ 30 TreeNode tmp = root.left; 31 root.left = root.right; 32 root.right = tmp; 33 Mirror(root.left); 34 Mirror(root.right); 35 } 36 } 37 38 private static int index = -1; 39 public static void main(String[] args) { 40 Integer[] array = {8,6,5,null,null,7,null,null,10,9,null,null,11,null,null}; 41 TreeNode tree = createBinTree(array); 42 // inOrder(tree); 43 // postOrder(tree); 44 Mirror(tree); 45 preOrder(tree); 46 } 47 private static TreeNode createBinTree(Integer[] array) { 48 TreeNode tree = null; 49 if(index < array.length-1 && array[++index]!=null){ 50 tree = new TreeNode(array[index]); 51 tree.left = createBinTree(array); 52 tree.right = createBinTree(array); 53 } 54 return tree; 55 } 56 private static void preOrder(TreeNode root){ 57 if(root!=null){ 58 System.out.print(root.val+" "); 59 preOrder(root.left); 60 preOrder(root.right); 61 } 62 } 63 64 private static void inOrder(TreeNode root){ 65 if(root!=null){ 66 preOrder(root.left); 67 System.out.print(root.val+" "); 68 preOrder(root.right); 69 } 70 } 71 72 private static void postOrder(TreeNode root){ 73 if(root!=null){ 74 preOrder(root.left); 75 preOrder(root.right); 76 System.out.print(root.val+" "); 77 } 78 } 79 }
1、创建二叉树,
2、先序遍历,
3、中序遍历,
4、后序遍历,
5、将二叉树变为镜像
标签:post integer length treenode string import tor mirror 镜像
原文地址:https://www.cnblogs.com/yangenyu/p/11299106.html