【劍指Offer】操作給定的二叉樹,將其變換為源二叉樹的鏡像。
阿新 • • 發佈:2018-10-27
right 鏡像 tree style turn val 交換 實現 oot
二叉樹的鏡像定義:源二叉樹
8
/ \
6 10
/ \ / \
5 7 9 11
鏡像二叉樹
8
/ \
10 6
/ \ / \
11 9 7 5
/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } }*/ //遞歸實現 public class Solution { public void Mirror(TreeNode root) { if(root==null){ return; } //將一個節點的左右節點交換 TreeNode node=root.left; root.left=root.right; root.right=node; if(root.left!=null){ Mirror(root.left); }if(root.right!=null){ Mirror(root.right); } } }
【劍指Offer】操作給定的二叉樹,將其變換為源二叉樹的鏡像。