156. Binary Tree Upside Down反转二叉树
[抄题]:
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.
Example:
Input: [1,2,3,4,5]
1
/ \
2 3
/ \
4 5
Output: return the root of the binary tree [4,5,2,#,#,3,1]
4
/ \
5 2
/ \
3 1
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[英文数据结构或算法,为什么不用别的数据结构或算法]:
二叉树中用left/right是recursive,类似图中的公式
一个个节点去写是iterative,类似图中的for循环
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
六步里面:要提前把temp节点存起来,传递给cur.left
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
TreeNode类这样写 另外加一个item参数 相当于新建指针了
然后solution里要包括一个root
class TreeNode
{
int data;
TreeNode left, right; //parameter is another item
TreeNode(int item) {
data = item;
left = right = null;
}
}
新建节点需要tree.root = new,调用数据需要.data
class MyCode {
public static void main (String[] args) {
Solution tree = new Solution();
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(2);
tree.root.right = new TreeNode(3);
tree.root.left.left = new TreeNode(4);
tree.root.left.right = new TreeNode(5);
TreeNode t = tree.upsideDownBinaryTree(tree.root);
System.out.println("t.root.data = " + t.data);
System.out.println("t.root.left.data = " + t.left.data);
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
// package whatever; // don't place package name! import java.io.*;
import java.util.*;
import java.lang.*; class TreeNode
{
int data;
TreeNode left, right; //parameter is another item
TreeNode(int item) {
data = item;
left = right = null;
}
} class Solution {
//new root
TreeNode root;
//method, need parameter, there is return
public TreeNode upsideDownBinaryTree(TreeNode root) {
//ini: prev, cur, next;
TreeNode cur = root;
TreeNode prev = null;
TreeNode temp = null;
TreeNode next = null; //iteration
while (cur != null) {
next = cur.left;
cur.left = temp;
temp = cur.right;
cur.right = prev; prev = cur;
cur = next;
} return prev;
}
} class MyCode {
public static void main (String[] args) {
Solution tree = new Solution();
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(2);
tree.root.right = new TreeNode(3);
tree.root.left.left = new TreeNode(4);
tree.root.left.right = new TreeNode(5); TreeNode t = tree.upsideDownBinaryTree(tree.root);
System.out.println("t.root.data = " + t.data);
System.out.println("t.root.left.data = " + t.left.data);
}
}
[潜台词] :
156. Binary Tree Upside Down反转二叉树的更多相关文章
- [leetcode]156.Binary Tree Upside Down颠倒二叉树
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java
156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions Total Accepted: ...
- [LeetCode] 156. Binary Tree Upside Down 二叉树的上下颠倒
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- 156. Binary Tree Upside Down
题目: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node ...
- 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- [LeetCode#156] Binary Tree Upside Down
Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left ...
- [LC] 156. Binary Tree Upside Down
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [Locked] Binary Tree Upside Down
Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a s ...
随机推荐
- 爬坑Linux
一.把cmd换成cmder 1.下载迷你版的cmder(因为win10的linux就是BASH,完整版的cmder也带这个,多余,还下载很慢),下载的是一个rar的文件,直接解压就可以了 2.将cmd ...
- nginx 文档链接
https://www.cnblogs.com/wcwnina/p/8728391.html NGINX简介 http://www.nginx.cn/doc/ ...
- mysql下载以及安装
因为xampp怎么都连接不上mysql,我感觉有可能是因为装mysql的时候试了很多次才安装成功,之前的mysql没有卸载干净造成的,今天把mysql卸载干净,又重新安装配置环境,但是还是连接不上,然 ...
- 使用pageoffice进行多个文档的合并
提前给test模板文件中 手动插入一个书签,因为pageoffice必须有一个书签后,才能在后台进行书签的创建 //多个word文件进行合并 string strCopyFolder = System ...
- Spark 性能调优-内存设置-GC设置
http://mt.sohu.com/20150604/n414449770.shtml http://my.oschina.net/mkh/blog/330386 http://itindex.ne ...
- Linux进程被杀掉(OOM killer),查看系统日志
基本概念: Linux 内核有个机制叫OOM killer(Out Of Memory killer),该机制会监控那些占用内存过大,尤其是瞬间占用内存很快的进程,然后防止内存耗尽而自动把该进程杀掉. ...
- Linux 本地repo配置
系统版本 centos6.9 配置方法 [local]name=localbaseurl=file:///home/systemimage/gpgcheck=1gpgkey=file:///etc/p ...
- Xtrabackup2.4.8备份、还原、恢复Mysql5.7.19实操(网络拷贝)
环境:CentOS 6.7 + Mysql 5.7.19 + Xtraback 2.4.8 innobackupex常用参数: --user=USER 指定备份用户,不指定的话为当前系统用户 --p ...
- JAVA 异常类型结构分析
JAVA 异常类型结构分析 Throwable 是所有异常类型的基类,Throwable 下一层分为两个分支,Error 和 Exception. Error 和 Exception Error Er ...
- Windows下文件加固
今天学到一种Windows下简单的文件加固方法,可以防止文件被(普通)删除. CMD找到要加固的文件. 例:C盘下有个 1516.txt 文本文件需要加固. 然后 copy 该文件.(注意:这里并非普 ...