二叉树翻转 · binary tree flipping
[抄题]:
给定一个二叉树,其中所有右节点要么是具有兄弟节点的叶节点(有一个共享相同父节点的左节点)或空白,将其倒置并将其转换为树,其中原来的右节点变为左叶子节点。返回新的根节点。
给出一个二叉树 {1,2,3,4,5}
1
/ \
2 3
/ \
4 5
返回二叉树的根 {4,5,2,#,#,3,1}
4
/ \
5 2
/ \
3 1
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
不知道怎么写dfs:先写总表达式bfs(某节点),再写具体操作。实际执行是调用-调用-调用-调用……直到从最底端节点开始。
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
如果先翻转2,指向新节点后会失去和原左右节点的联系,导致断层。
如果先翻转6,没有左右节点,指向新节点后会失去和原左右节点的联系,也没关系。
[一刷]:
- bfs和主函数都要写各自的特判返回,bfs的特判和内容都是有继承关系的下一个点
- 定义一个成员变量newroot,在void型bfs的特殊判断中发生联系
[二刷]:
- 新树的左右是相反的,需要倒过来看。
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
就是用bfs再走一次
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
public class Solution {
/*
* @param root: the root of binary tree
* @return: new root
*/
TreeNode newRoot;
//bfs
//corner case
void bfs(TreeNode curt) {
if (curt.left == null) {
newRoot = curt;
return ;
}
bfs(curt.left);
curt.left.right = curt;
curt.left.left = curt.right;
curt.left = null;
curt.right = null;
} public TreeNode upsideDownBinaryTree(TreeNode root) {
if (root == null) {
return null;
}
bfs(root);
return newRoot;
}
}
二叉树翻转 · binary tree flipping的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化
遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...
- 数据结构-二叉树(Binary Tree)
1.二叉树(Binary Tree) 是n(n>=0)个结点的有限集合,该集合或者为空集(空二叉树),或者由一个根节点和两棵互不相交的,分别称为根节点的左子树和右子树的二叉树组成. 2.特数二 ...
- [Swift]LeetCode226. 翻转二叉树 | Invert Binary Tree
Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...
- [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [Swift]LeetCode654. 最大二叉树 | Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- [Swift]LeetCode655. 输出二叉树 | Print Binary Tree
Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...
- [Swift]LeetCode814. 二叉树剪枝 | Binary Tree Pruning
We are given the head node root of a binary tree, where additionally every node's value is either a ...
随机推荐
- SoftMax多分类器原理及代码理解
关于多分类 我们常见的逻辑回归.SVM等常用于解决二分类问题,对于多分类问题,比如识别手写数字,它就需要10个分类,同样也可以用逻辑回归或SVM,只是需要多个二分类来组成多分类,但这里讨论另外一种方式 ...
- 小数第n位
问题描述 我们知道,整数做除法时,有时得到有限小数,有时得到无限循环小数. 如果我们把有限小数的末尾加上无限多个0,它们就有了统一的形式. 本题的任务是:在上面的约定下,求整数除法小数点后的第n位开始 ...
- fackbook flow 简单使用
flow 是一个javascript 静态检查的工具,由facebook 开发, 使用起来简单,方便. 安装 项目初始化 yarn init -y 编译器安装 yarn add --dev babel ...
- Java多线程编程核心技术,第六章
1,饿汉模式/单例模式,一开始就新建一个静态变量,后面用getInstance()都是同一个变量 2,懒汉模式/单例模式,在getInstance()才会new一个对象,在第一个有了后不会继续创建 3 ...
- CentOS 6.5 下搭建vsftp服务
参考网站: http://blog.163.com/sunshine_linting/blog/static/44893323201391010522601/ http://blog.sina.com ...
- oracle undo表空间大小修改
redhat:清空回收站 rm -rf /home/登录用户名/.Trash 例子:rm -rf /home/.Trash-root df命令可以显示目前所有文件系统的可用空间及使用情形: 例子:d ...
- unity里面的gameobject和transform的关系
一切都是物体(gameobject),而transform是物体的一个基本属性类,包含位置,旋转,缩放,三个基本属性,两者之间可以互相转换 查找物体,建议用transform,GameObject无法 ...
- hudson插件说明
Artifactory Plugin:maven仓库管理工具 Backup plugin 可以备份hudson_home下所有文件,除了svncode.这个插件有问题,不能使用. Build Publ ...
- [搬运] [贪心]NOIP2011 观光公交
推荐这篇题解:http://www.cnblogs.com/Blacko/archive/2013/10/18/3376597.html 只不过这篇题解有一些细节没有说清,但建议自己思考- Codes ...
- 10-28质量监控ELK
监控业务范围 app崩溃监控(Bugly) 应用性能监控(APM) 业务监控(TalkingData.友盟) 质量监控(缺位) 质量监控平台ELK elk官网 数据构造 线上错误状态分布 故障影响范围 ...