劍指offer之 二叉樹鏡像
阿新 • • 發佈:2017-10-12
tno eno 描述 temp 結點 輸出 urn tree 定義
package Problem19; /* * 問題描述: * 請完成一個函數,輸入一個二叉樹,該函數輸出它的鏡像; */ //定義二叉樹的結構 class BinaryTreeNode { BinaryTreeNode leftNode; BinaryTreeNode rightNode; int value; } public class MirrorOfBinaryTree { public static void mirroOfBinTree(BinaryTreeNode root) { if (root == null) { return; } if (root.leftNode == null || root.rightNode == null) { return; } // 交換一個結點的左右子節點 int tempValue; tempValue = root.leftNode.value; root.leftNode.value = root.rightNode.value; root.rightNode.value = tempValue; // 遞歸操作左右子節點 if (root.leftNode != null) { mirroOfBinTree(root.leftNode); } if (root.rightNode != null) { mirroOfBinTree(root.rightNode); } }
劍指offer之 二叉樹鏡像