[抄题]:

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

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

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. 转 关于nvcc fatal : Value 'sm_20' is not defined for option 'gpu-architecture'的问题

    原文地址: https://blog.csdn.net/Mao_Jonah/article/details/78965827 关于nvcc fatal : Value ‘sm_20’ is not d ...

  2. ZetCode PyQt4 tutorial signals and slots

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  3. VUE的使用方法

    vueInit: function() { var _this = this; this.vue = new Vue({ el: '#pa', data: { //存放初始化数据 sourceData ...

  4. Visual Studio 2005 自带单元测试

    一 单元测试简介   单元测试是代码正确性验证的最重要的工具,也是系统测试当中最重要的环节.也是唯一需要编写代码才能进行测试的一种测试方法.在标准的开发过程中,单元测试的代码与实际程序的代码具有同等的 ...

  5. centos 7 安装rabbitmq 3.6.12

    0 安装 epel yum -y install http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11. ...

  6. linux系统报错日志学习

    linux本身会自动记录系统报错日志:/var/log/messages 这个日志记录,我是在什么时候发现其强大的作用的呢?它有点像我们使用php脚本开发接口的时候技术员在重要地方打日志的效果,方便技 ...

  7. 复制mysql数据库的步骤

    Navicat 转存sql文件 然后命令 mysql -uroot -p123456 dbname < e:/backup/20141014.sql

  8. python之CMDB

    浅谈ITIL TIL即IT基础架构库(Information Technology Infrastructure Library, ITIL,信息技术基础架构库)由英国政府部门CCTA(Central ...

  9. 20165233 Java第五、六章学习总结

    20165233 2017-2018-2 <Java程序设计>第四周学习总结 教材学习内容总结 ch05 子类与父类以及子类的继承性 一个子类只能有一个父类 使用extends关键字定义子 ...

  10. Git 知识总结

    版本控制git之一 - 仓库管理 版本控制git之二-分支 git https://mp.weixin.qq.com/s/96FS12DTzbjAJQ1ynRNqdg git init 初始化目录 g ...