二叉树的镜像、对称的二叉树---Java实现

版权声明:转载请标明附带连接标明出处 https://blog.csdn.net/Hollake/article/details/89398054

二叉树的镜像

题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。

输入描述:
二叉树的镜像定义:
原二叉树 :
    	    8
    	   /  \
    	  6   10
    	 / \  / \
    	5  7 9 11
镜像二叉树:
    	    8
    	   /  \
    	  10   6
    	 / \  / \
    	11 9 7  5

思路分析:

先前序遍历这棵树的每个结点,如果遍历的节点有子节点,那么就进行左右子节点的交换。

递归实现:

public void Mirror(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNode tmp = null;
        
        tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        
        if (root.left != null ){
            Mirror(root.left);
        }
        if (root.right != null){
            Mirror(root.right);
        }
    }

非递归实现: 

public void Mirror(TreeNode pRoot) {
        if (pRoot == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(pRoot);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            if (node.left != null || node.right != null) {
                TreeNode temp = node.left;
                node.left = node.right;
                node.right = temp;
            }
            if (node.left != null) {
                stack.push(node.left);
            }
            if (node.right != null) {
                stack.push(node.right);
            }
        }
    }

对称的二叉树

题目描述:

请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

思路分析:

例如一下二叉树,这就是个典型的对称二叉树。对称二叉树是必须满足镜像对称,也就是从根节点中间切开,对折可以重合。即:根节点的左子树和右子树相同,左子树的左子树和右子树的右子树相等,左子树的右子树和右子树的左子树相等,可以采用递归和非递归两种方法实现。

            6
          /   \
         8     8
        / \   / \
       1   3 3   1

递归实现:

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val){
        this.val = val;
    }
}

public static boolean  isSymmetrical(TreeNode pRoot)
    {
        if (pRoot == null) {
            return true;
        }
        return comRoot(pRoot.left, pRoot.right);

    }

    private static boolean comRoot(TreeNode left, TreeNode right) {
        if (left == null)
            return right == null;
        //执行到这里已经说明left!=null
        if (right == null)
            return false;
        //这里千万别大意写成left != right,这样肯定会一直输出false,因为比较的是对象
        if (left.val != right.val)
            return false;
        //如果左子树和右子树的根节点相等,继续递归比较它们左子树的左子树节点和右子树的右子树节点,
        //以及左子树的右子树节点和右子树的左子树节点。
        return comRoot(left.left, right.right) && comRoot(left.right, right.left);
    }

非递归代码实现:

非递归实现的思路就是将每一对称节点的左子树和和右子树,右子树和左子树添加到队列进行判断,注意这里空节点即:null会添加到队列中,但是如果取出的两个元素为空,那么结束当前while循环,重新进行取值。

public static boolean isSymmetrical(TreeNode pRoot){
        if(pRoot == null) return true;
        LinkedList<TreeNode> queue = new LinkedList<>();
        //元素为空也会添加入队列
        queue.add(pRoot.left);
        queue.add(pRoot.right);
        while(!queue.isEmpty()) {
            TreeNode left = queue.pop();//成对取出
            TreeNode right = queue.pop();
            //如果成对取出的两个元素均为空,当前while循环结束,重新再队列中取元素
            if(left == null && right == null) continue;
            if(left == null || right == null) return false;
            if(left.val != right.val) return false;
            //成对插入
            queue.add(left.left);
            queue.add(right.right);
            queue.add(left.right);
            queue.add(right.left);
        }
        return true;
    }

猜你喜欢

转载自blog.csdn.net/Hollake/article/details/89398054