Invert a binary tree 翻转一棵二叉树
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 翻转一棵二叉树的更多相关文章
- 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...
- [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 ...
- lintcode :Invert Binary Tree 翻转二叉树
题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 226. Invert Binary Tree 翻转二叉树
[抄题]: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 [暴力解法]: 时间分析: 空间分 ...
- 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 ...
- PAT-1102(Invert a Binary Tree)+二叉树的镜像+层次遍历+中序遍历+已知树的结构构树
Invert a Binary Tree pat-1102 import java.util.Arrays; import java.util.Queue; import java.util.Scan ...
- 1102 Invert a Binary Tree——PAT甲级真题
1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...
- PAT1102: Invert a Binary Tree
1102. Invert a Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
随机推荐
- Java泛型学习
1.泛型的概念 泛型即"参数化类型",就比如我们定义方法的时候,定义一个变量,称为形参,变量值根据传进去的实参的值不同而改变.而泛型的出现,就是为了解决类型也能根据传进去的类型改变 ...
- Java常用类之String类练习
1.编程. 已知字符串:"this is a test of java". 按要求执行以下操作: (1) 统计该字符串中字母s出现的次数 (2) 取出子字符串"test& ...
- nodeJS之TCP模块net
前面的话 TCP服务在网络应用中十分常见,目前大多数的应用都是基于TCP搭建而成的.net模块提供了一个异步网络包装器,用于TCP网络编程,它包含了创建服务器和客户端的方法.本文将详细介绍nodeJS ...
- php注册登录源代码
php注册登录源代码 链接数据库<?php$conn=mysql_connect('localhost','root','');mysql_select_db('ht',$conn);mysql ...
- linux 系统备份日志
题目: 备份日志 小明是一个服务器管理员,他需要每天备份论坛数据(这里我们用日志替代),备份当天的日志并删除之前的日志.而且备份之后文件名是年-月-日的格式.alternatives.log在/var ...
- 解决Centos crontab没有按时运行的问题
我装了centos,用一点一直无法搞定,就是定时关机.我只是想做这一件事: 每天凌晨1点自动关机 0 1 * * * shutdown now -h 然而,无论我如何设置,都是失败. 每当我睡了一觉, ...
- rest api get 查询接口 多个参数
查询时,使用get,传递参数至服务器. angular js中,$http可以直接传递object,在get中,params:data 在服务端, query(x=x,y=y)可写成 query(** ...
- 记一次linux主机名莫名其妙变成了bogon
起因:公司网络接口做了接口认证,虚拟机桥接至物理网卡无法完成认证进行网络访问,无奈之下只能讲虚拟机网络模式更改为NAT模式,更改完成之后进行ssh登录,发现主机名发生了变化. 更改NAT模式之前 [r ...
- 门控开关项目--使用Multism仿真
可以修改桥式整流电路的电容,发现容值不合适的时候,直流不平稳.
- require.js详解
一:什么是require.js ①:require.js是一个js脚本加载器,它遵循AMD(Asynchronous Module Definition)规范,实现js脚本的异步加载,不阻塞页面的渲染 ...