zoukankan      html  css  js  c++  java
  • 剑指offer18题

    class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
    
        public TreeNode(int val) {
            this.val = val;
    
        }
    }
    
    /**
     * 操作给定的二叉树,将其变换为源二叉树的镜像。
     * 思路:
     * 1、逐层深入,把左右两边交换
     *
     * code:
     */
    
    public class Solution18 {
        public void Mirror(TreeNode root) {
            if (root == null) {
                return;
            }
            exchange(root);
        }
    
        private void exchange(TreeNode root) {
            doExchange(root);
            if (root.left != null) {
                exchange(root.left);
            }
            if (root.right != null) {
                exchange(root.right);
            }
        }
    
        private void doExchange(TreeNode root) {
            if (root == null) {
                return;
            }
            TreeNode temp;
            temp = root.left;
            root.left = root.right;
            root.right = temp;
        }
    
    }
  • 相关阅读:
    I/O流
    宇宙第一帅的HTML笔记
    宇宙无敌第一帅的Java笔记
    Activity常用的方法
    Spinne
    安卓布局方式
    for循环
    TextView
    开发Activity步骤
    重写
  • 原文地址:https://www.cnblogs.com/Adam-Ye/p/13466268.html
Copyright © 2011-2022 走看看