首页 > 其他 > 详细

18.镜像二叉树

时间:2019-04-06 18:15:26      阅读:114      评论:0      收藏:0      [点我收藏+]

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

/**
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 temp
= null; temp = root.right; root.right = root.left; root.left = temp; if(root.left != null) {
        //换成左节点 Mirror(root.left); }
if(root.right != null) {
        //换成右节点 Mirror(root.right); } } }

 

18.镜像二叉树

原文:https://www.cnblogs.com/wzQingtTian/p/10662325.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!