20200326 - 검지offer 면접문제 27: 두 갈래 나무의 거울

7274 단어 검지offer
package question28_is_mirror;

/**
 * @Classname Solution
 * @Description TODO
 * @Date 2020/3/27 0:04
 * @Created by mmz
 */
public class Solution {

    class TreeNode{
        int val;
        TreeNode left = null   ;
        TreeNode right =  null  ;
    }

     public boolean isMirror(TreeNode root){
        return isMirrorLR(root,root);
     }

     public boolean isMirrorLR(TreeNode left,TreeNode right){
        if(left == null && right == null){
            return true;
        }
        if(left == null || right == null){
            return false;
        }
        if(left.val != right.val){
            return  false;
        }
        return isMirrorLR(left.left,right.right) &&isMirrorLR(left.right,right.left);
     }
}


이솔
package question27_ ;

import sun.reflect.generics.tree.Tree;

/**
 * @Classname Main
 * @Description TODO
 * @Date 2020/4/11 22:16
 * @Created by mmz
 */
public class Main {
    static class  TreeNode{
        int val;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "val=" + val +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }

    static TreeNode Core(TreeNode old){
        if(old == null){
            return null;
        }
        TreeNode newroot  = new TreeNode(old.val);
        newroot.right =  Core(old.left);
        newroot.left = Core(old.right);
        return newroot;
    }

    public static void main(String[] args) {
        TreeNode one = new TreeNode(8);
        TreeNode two = new TreeNode(6);
        TreeNode three =new TreeNode(10);
        one.right = three;
        one.left = two;

        System.out.println(Core(one));
    }
}


위 안에 28문제의 답안이 있는데 어떻게 꼬치는지 모르겠다.간단해.

좋은 웹페이지 즐겨찾기