[抄题]:

给定一个二叉树,其中所有右节点要么是具有兄弟节点的叶节点(有一个共享相同父节点的左节点)或空白,将其倒置并将其转换为树,其中原来的右节点变为左叶子节点。返回新的根节点。

您在真实的面试中是否遇到过这个题?

Yes
样例

给出一个二叉树 {1,2,3,4,5}

    1
/ \
2 3
/ \
4 5

返回二叉树的根 {4,5,2,#,#,3,1}

    4
/ \
5 2
/ \
3 1

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

不知道怎么写dfs:先写总表达式bfs(某节点),再写具体操作。实际执行是调用-调用-调用-调用……直到从最底端节点开始。

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

如果先翻转2,指向新节点后会失去和原左右节点的联系,导致断层。

如果先翻转6,没有左右节点,指向新节点后会失去和原左右节点的联系,也没关系。

[一刷]:

  1. bfs和主函数都要写各自的特判返回,bfs的特判和内容都是有继承关系的下一个点
  2. 定义一个成员变量newroot,在void型bfs的特殊判断中发生联系

[二刷]:

  1. 新树的左右是相反的,需要倒过来看。

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼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的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  3. 数据结构-二叉树(Binary Tree)

    1.二叉树(Binary Tree) 是n(n>=0)个结点的有限集合,该集合或者为空集(空二叉树),或者由一个根节点和两棵互不相交的,分别称为根节点的左子树和右子树的二叉树组成.  2.特数二 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [Swift]LeetCode654. 最大二叉树 | Maximum Binary Tree

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Mac安装并破解OmniGraffle7

    这个实际上不算破解,只是找到了可用的序列号 1.下载地址 https://www.omnigroup.com/omnigraffle 2.激活方法 Omnigraffle Pro 7注册码/许可证 名 ...

  2. cousera 深度学习 吴恩达 第一课 第二周 学习率对优化结果的影响

    本文代码实验地址: https://github.com/guojun007/logistic_regression_learning_rate cousera 上的作业是 编写一个 logistic ...

  3. Jmeter-Logic Controllers(逻辑控制器)

    Critical Section Controller(临界区控制器) 参考:http://www.cnblogs.com/yanzhe/p/7729984.html ForEach Controll ...

  4. k8s PersistentVolume hostpath 简单使用

    kubernets host PersistentVolume 测试 因为yaml 格式的问题 ,我修改为了json 创建 pv pv.json { "kind": "P ...

  5. 再看Spring Could微服务的关键组件

    Consul是用go开发的开源注册中心服务,内置服务发现与注册.raft一致性协议实现.健康检查.多数据中心等方案.与Eurker相比,consul还能对异构服务如rpc提供支持. 作为微服务系统的核 ...

  6. oracle12c之 表空间维护总结

    1.1.创建永久表空间 In the CDB:SQL> CONNECT system@cdb1SQL> CREATE TABLESPACE cdb_users DATAFILE'/home ...

  7. 【python】 Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat

    刚在机器上windows环境下装上pip方便以后安装包的时候使用,谁知道第一次使用pip安装asyncio的时候就报错. 在Windows7x64下使用pip安装包的时候提示报错:Microsoft ...

  8. 全是干货!UI设计的30条黄金准则!

    http://www.wex5.com/portfolio-items/js-1/ 全是干货!UI设计的30条黄金准则!   总的来说,好的UI界面有几个特征:简洁.便利.目标明确.人性化.字面上看这 ...

  9. python学习日志

    马上就中秋节,想着再学点新的知识,本来想去继续研究前端知识来着,但是内个烦人的样式css还有js搞的有点脑壳头,以后就主学后端吧,要去死了前端这条心了? 那么寻寻觅觅就入坑最近几年大热的python吧 ...

  10. <meta name="viewport" content="width=device-width, initial-scale=1.0">的说明

    今天在做适配手机版时,chrome调到手机版,但是还是显示PC端的样式,无法展现出手机端的样式: 开始的时候还以为是chrome版本的问题,最新版本的chrome62.0是有很多变化的,而之前工作中使 ...