zoukankan      html  css  js  c++  java
  • 剑指offer——二叉树的镜像

    题目链接:操作给定的二叉树,将其变换为源二叉树的镜像。

    解题思路:应用递归,先交换左子树与右子树,然后再交换左子树的子节点和右子树的子节点。

     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 import java.util.Stack;
    15 public class Solution {
    16     public void Mirror(TreeNode root) {
    17         
    18         if(root==null)
    19             return ;
    20         if(root.left==null && root.right==null)
    21             return ;
    22         TreeNode temp = root.left;
    23         root.left=root.right;
    24         root.right= temp;
    25         
    26         if(root.left!=null)
    27             Mirror(root.left);
    28         if(root.right!=null)
    29             Mirror(root.right);
    30             
    31         
    32     }
    33 }
  • 相关阅读:
    git
    fragment
    Builder模式
    代码混淆
    android studio快捷键
    小知识点
    angular组件使用
    英语摘要2019-6-4
    英语笔记2019-4-3
    搭建Eureka注册中心时遇到的问题
  • 原文地址:https://www.cnblogs.com/wangyufeiaichiyu/p/10860649.html
Copyright © 2011-2022 走看看