劍指offer28題
阿新 • • 發佈:2017-08-10
algo nbsp desc 輸入 強行 span mirror oot 鏡像二叉樹
題目描述
操作給定的二叉樹,將其變換為源二叉樹的鏡像。輸入描述:
二叉樹的鏡像定義:源二叉樹 8 / 6 10 / \ / 5 7 9 11 鏡像二叉樹 8 / 10 6 / \ / 11 9 7 5
廢話不多說,直接強行上代碼。樹的創建和打印已經封裝好,直接調用。
package com.algorithm04; import com.tools.TreeBinaryFunction; import com.tools.TreeNode;public class Algorithm27 { public static void Mirror(TreeNode root){ if(root!=null){ TreeNode temp = new TreeNode(0); 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 static void main(String[] args) { TreeNode treeNode = new TreeNode(0); treeNode = TreeBinaryFunction.CreateTreeBinary(treeNode); Mirror(treeNode); TreeBinaryFunction.PrintTreeBinary(treeNode); } }
劍指offer28題