[抄题]:

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

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

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. SOA实践指南-读书笔记

    SOA是英文Service-Oriented Architecture,即面向服务架构的缩写. SOA是一种范式,目的是增强灵活性.SOA很适宜处理复杂的分布式系统. SOA方法接受异质(不同的平台, ...

  2. Eclipse添加中文javadoc

    SUN官方API中文版[JDK1.6]1.6API文档(中文)的下载地址:ZIP格式用来设置javadoc,下载地址:http://download.java.net/jdk/jdk-api-loca ...

  3. python学习(一)—简明python教程

    2016-04-12 15:59:47 1. 介绍2. 安装Python3. 最初的步骤4. 基本概念5. 运算符与表达式6. 控制流7. 函数8. 模块9. 数据结构10. 解决问题——编写一个Py ...

  4. An internal error occurred during: "Android Library Update".

    打开 proj.android下的.cproject文件,把多余的CConfiguration配置节删掉,只留第一个就行了.

  5. 一:线性dp

    概念: 动态规划是运筹学的一个分支,是求解决策过程最优化的数学方法. 动态规划是通过拆分问题,定义问题状态和状态之间的关系使得问题能够以递推(或者说分治)的方法去解决. 解决策略: 1)最优化原理:如 ...

  6. linux系统硬件配置

    转自:http://blog.163.com/yang_jianli/blog/static/1619900062010391127338/ 一:查看cpu more /proc/cpuinfo | ...

  7. c# 设置自动隐藏任务栏、获取状态

    from: http://stackoverflow.com/questions/1381821/how-to-toggle-switch-windows-taskbar-from-show-to-a ...

  8. map、reduce处理数据结构及常见案例

    随着三大前端框架和小程序的流行,MVVM大行其道,而其中的核心是 ViewModel 层,它就像是一个中转站(value converter),负责转换 Model 中的数据对象来让数据变得更容易管理 ...

  9. java之序列化与反序列化

    1.这里主要是介绍Protobuf提供的序列化与反序列化的高效性.相对于传统的java提供的序列化来说,Protobuf的效率提高了很多倍.但是也有不足的地方,就是proto在对象序列化的时候抛弃了很 ...

  10. JavaScript Promise的学习笔记

    首先声明:本人今天刚接触Promise,通过一个例子,希望能更好的来理解,如果有不对的地方,还望指正 Promise是专门为解决 js中回调而引起的各种问题,而产生的. 在异步编程中,我们经常使用回调 ...