Invert a binary tree 翻转一棵二叉树

假设有如下一棵二叉树:
  4
   / \
   2    7
  / \   / \
 1  3 6  9
翻转后:

4
     /    \
    7     2
   / \    / \
  9  6  3  1

这里采用递归的方法来处理。遍历结点,将每个结点的两个子结点交换位置即可。
从左子树开始,层层深入,由底向上处理结点的左右子结点;然后再处理右子树

全部代码如下:

public class InvertBinaryTree {

    public static void main(String args[]) {
        TreeNode root = new TreeNode(0); // 构建简单的二叉树
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node5;
        node2.right = node6;
        node4.right = node7;

        preOrderTravels(root);          // 前序遍历一次
        System.out.println();
        root = invertBinaryTree(root);
        preOrderTravels(root);          // 翻转后再前序遍历一次

    }
    // 前序遍历
    public static void preOrderTravels(TreeNode node) {
        if (node == null) {
            return;
        } else {
            System.out.print(node.val + " ");
            preOrderTravels(node.left);
            preOrderTravels(node.right);
        }
    }
    // 翻转二叉树
    private static TreeNode invertBinaryTree(TreeNode root) {
        if (root == null|| (root.left == null && root.right == null))
            return root;// 为空,或没有子树,直接返回
        TreeNode tmp = root.right;               // 右子树存入tmp中
        root.right = invertBinaryTree(root.left);// 先处理左子树,然后接到root的右链接
        root.left = invertBinaryTree(tmp);       // 处理tmp中原来的右子树,然后接到root的左链接
        return root;
    }
}

输出:

0 1 3 4 7 2 5 6
0 2 6 5 1 4 7 3

Invert a binary tree 翻转一棵二叉树的更多相关文章

  1. 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...

  2. [LeetCode] Invert Binary Tree 翻转二叉树

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...

  3. lintcode :Invert Binary Tree 翻转二叉树

    题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...

  4. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  5. 226. Invert Binary Tree 翻转二叉树

    [抄题]: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 [暴力解法]: 时间分析: 空间分 ...

  6. leetcode 226 Invert Binary Tree 翻转二叉树

    大牛没有能做出来的题,我们要好好做一做 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Tri ...

  7. PAT-1102(Invert a Binary Tree)+二叉树的镜像+层次遍历+中序遍历+已知树的结构构树

    Invert a Binary Tree pat-1102 import java.util.Arrays; import java.util.Queue; import java.util.Scan ...

  8. 1102 Invert a Binary Tree——PAT甲级真题

    1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...

  9. PAT1102: Invert a Binary Tree

    1102. Invert a Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

随机推荐

  1. (转+原创)java的枚举类型Enum解释

    原文:http://www.cnblogs.com/mxmbk/articles/5091999.html 下文中还添加了个人的一些补充和理解. 在Java SE5之前,我们要使用枚举类型时,通常会使 ...

  2. 一键帮你复制多个文件到多个机器——PowerShell小脚本(内附PS远程执行命令问题解析)

    作为一个后台程序猿,经常需要把一堆程序集(DLL)或者应用程序(EXE)复制到多个服务器上,实现程序的代码逻辑更新,用以测试新的功能或改动逻辑.这里给大家介绍一个自己实现的PowerShell脚本,方 ...

  3. PHP中的面向对象OOP中的魔术方法

    一.什么是魔术方法: PHP为我们提供了一系列用__开头的函数,这些函数无需自己手动调用,会在合适的时机自动调用,这类函数称为魔术函数.例如: function __construct(){} 在ne ...

  4. 包装FTPWebRequest类

    上篇文章讨论了C#从基于FTPS的FTP server下载数据 (FtpWebRequest 的使用)SSL 加密.不过细心的朋友应该可以发现FTPWebRequest 每次都是新生成一个reques ...

  5. 关于控制下拉框只读的js控制

    文本框有readonly属性,直接设置:下拉框没有readonly属性,也不能通过其他属性进行只读的设置,下拉框只有disabled属性,但是这个属性设成true之后,值就获取不到了: 我在网上搜了一 ...

  6. java调用(axis2)WebService传递对象类型参数(源码)

    温馨提示:axis2 jar包哟 public static String pubRemoteFuc() {                String endpoint = "http:/ ...

  7. node项目的基本构建流程或者打开一个node项目的流程

    1.  确立项目所需要的所有依赖.框架(比如bootstrap,vue,angular等) 2. 在项目的根目录下创建一个package.json文件,package.json文件是项目的最重要文件之 ...

  8. API 接口规范

    整体规范建议采用RESTful 方式来实施. 1. 协议 API与用户的通信协议,总是使用HTTPs协议,确保交互数据的传输安全. 2. 域名 应该尽量将API部署在专用域名之下. https://a ...

  9. MongoDB--数据库管理

    <strong>1.mongod 启动项注释(默认不能生成路径,需手动建立)</strong><br /><span style="white-sp ...

  10. java编码详解

    举个例子 我们在开发过程中,特别是多种编码格式并存的情况下,很容易遇到乱码问题. 假如有一个GBK编码java文件,然后再使用-Dfile.encoding=GBK参数,写入的文件中哪些是乱码呢.那如 ...