검지 offer--두 갈래 나무의 거울

3469 단어 검지offer

제목 설명


주어진 두 갈래 트리를 조작하여 원본 두 갈래 트리의 거울로 변환합니다.입력 설명: 두 갈래 트리의 대칭복사 정의:
  
	    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;
        }
        if(root.left == null && root.right == 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);
    }
}

좋은 웹페이지 즐겨찾기