二叉树翻转 · 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 ...
随机推荐
- Eclipse 中 ctrl+鼠标左键 快捷查看资源失效
Eclipse 中 ctrl+鼠标左键 快捷查看资源失效 看看eclipse 工作空间配置的jdk是什么版本,看看本地环境变量中的jdk是什么版本,将二者的版本统一即可. 笔者从git上clone一个 ...
- 斯特灵(Stirling)数
http://zh.wikipedia.org/wiki/%E6%96%AF%E7%89%B9%E7%81%B5%E6%95%B0 第一类:n个元素分成k个非空循环排列(环)的方法总数 递推式:s(n ...
- Linux上jdk安装及环境变量设置
1.jdk下载和安装 (1)http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html下载需 ...
- ubuntu16.04安装Nvidia显卡驱动、CUDA8.0和cudNN V6
Nvidia显卡驱动安装 在ubuntu搜索框输入 软件更新,打开 "软件和更新" 对话框,在 附加驱动里选择系统检测到的Nvidia驱动,应用更改,重启系统: 安装完成之后查看G ...
- PyalgoTrade 打印收盘价(二)
让我们从一个简单的策略开始,就是在打印收盘价格的过程中: from pyalgotrade import strategy from pyalgotrade.barfeed import yahoof ...
- MongoDB数据库的特点以及结构
mongodb标签:非关系型数据库 文档型数据库 最像关系型的非关系型数据库 特点: 1. 由c++编写的数据库管理系统 2. 支持丰富的增删改查功能 3. 支持丰富的 ...
- 浅谈SQL Server---2
浅谈SQL Server内部运行机制 https://www.cnblogs.com/wangjiming/p/10098061.html 对于已经很熟悉T-SQL的读者,或者对于较专业的DBA来说, ...
- RabbitMQ消息队列安装
[root@VM_119_179_centos ~]# rpm -ivh erlang-19.0.4-1.el6.x86_64.rpm [root@VM_119_179_centos ~]# rpm ...
- 【转】Windows消息投递流程:一般窗口消息投递(WM_LBUTTONCLICK)
原文网址:http://blog.csdn.net/hyhnoproblem/article/details/6182646 本例通过在单文档程序的视图中添加WM_LBUTTONCLICK消息处理函数 ...
- xmlns和xsi之schemaLocation
appplicationContex.xml文件报错:元素 "util:constant" 的前缀 "util" 未绑定 在根节点添加了“xmlns:util= ...