【树】二叉树的镜像

题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。
比如:    源二叉树8/  6   10/ \  / 5  7 9 11镜像二叉树8/  10   6/ \  / 11 9 7  5

在这里插入图片描述


思路很简单,只需要递归遍历树,然后将每个节点的左右子树调换即可

import java.util.*;


public class Solution {
    
    
 
    public TreeNode Mirror(TreeNode pRoot) {
    
    
        // write code here
        if (pRoot != null) {
    
    
            TreeNode tmp = pRoot.left;
            pRoot.left = pRoot.right;
            pRoot.right = tmp;
            Mirror(pRoot.left);
            Mirror(pRoot.right);
        }
        return pRoot;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43486780/article/details/115047576
今日推荐