码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树的镜像

时间:2018-03-08 02:51:22      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:span   temp   col   树的镜像   bsp   treenode   gpo   镜像   this   

解题思路

  注意遍历结束的条件

问题描述

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

代码实现

 1 /**
 2 public class TreeNode {
 3     int val = 0;
 4     TreeNode left = null;
 5     TreeNode right = null;
 6 
 7     public TreeNode(int val) {
 8         this.val = val;
 9 
10     }
11 
12 }
13 */
14 public class Solution {
15     public void Mirror(TreeNode root) {
16         if(root == null || (root.left == null && root.right == null)) return ;
17         TreeNode temp = root.left;
18         root.left = root.right;
19         root.right = temp;
20         if(root.left!=null) Mirror(root.left);
21         if(root.right!=null) Mirror(root.right);
22     }
23 }

 

二叉树的镜像

标签:span   temp   col   树的镜像   bsp   treenode   gpo   镜像   this   

原文地址:https://www.cnblogs.com/wanglinyu/p/8525926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!