二叉樹的鏡像

題目描述

操作給定的二叉樹,將其變換爲源二叉樹的鏡像。

輸入描述:

二叉樹的鏡像定義:源二叉樹 
    	    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){
            TreeNode tmp = root.right;
            root.right = root.left;
            root.left = tmp;
            
            if(root.left != null) Mirror(root.left);
            if(root.right != null) Mirror(root.right);
        
        }

    }
    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章