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. GPU编程--Shared Memory(4)

    GPU的内存按照所属对象大致分为三类:线程独有的.block共享的.全局共享的.细分的话,包含global, local, shared, constant, and texture memoey, ...

  2. 图表(Chart & Graph)你真的用对了吗?

    欢迎大家持续关注葡萄城控件技术团队博客,更多更好的原创文章尽在这里~~ 工作中,我们常常会遇到各式各样的数据,例如网站性能,销售业绩,客户服务 .营销活动等数据.对于这些数据,有哪些行之有效的方法来形 ...

  3. HTMLCollection 对象详解,以及为什么循环获取的dom合集操作可能会出现下标不正确的情况?

    有时候循环dom合集,然后操作其中的某些dom之后,发现下标不正确了 比如我们要删除一个dom合集的时候: var selectDom = document.getElementsByClassNam ...

  4. 【easyui】Tab的tools按钮刷新当前tab

    点击刷新按钮,刷新当前Tab选项卡 /** * Name 选项卡初始化 */ $('#home-tabs').tabs({ tools: [{ iconCls: 'icon-reload', bord ...

  5. (整理)使用tomcat搭建HTTP文件下载服务器

    本文是整理,非原创,由网络资料组成上自己踩的坑整理而成. 1. 假设需要下载的文件目录是D:\download1(注意这里写了个1,跟后面的名称区分) 2. 设置 tomcat 的虚拟目录.在 {to ...

  6. jsonp跨域再谈

    昨天面试雷锋网,问到了jsonp跨域的问题,其实这个问题之前就会的,没有多大的深入,记得有一个名词在跨域中出现,就是同源机制, JavaScript是一种在Web开发中经常使用的前端动态脚本技术.在J ...

  7. 关于cas server无法通过session持久化方式实现集群的问题

    最近在搭建cas单点登录系统 ,在建立集群时发生一个问题. 搭建的环境是tomcat+tomcat-redis-session-manager+redis+cas 在对tomcat的session进行 ...

  8. python str转dict

    两种方法 捷径 eval(str) >>> user = "{'name' : 'jim', 'sex' : 'male', 'age': 18}" >&g ...

  9. 短信发送接口被恶意访问的网络攻击事件(三)定位恶意IP的日志分析脚本

    前言 承接前文<短信发送接口被恶意访问的网络攻击事件(二)肉搏战-阻止恶意请求>,文中有讲到一个定位非法IP的shell脚本,现在就来公布一下吧,并没有什么技术难度,只是当时花了些时间去写 ...

  10. Discuz添加自定义模板广告

    在做Discuz中广告的时候碰到个大问题,现在我需要做一个轮播的通屏广告位,调用广告图片的代码应该是以下代码:<ul>     <li style="background: ...