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 ...
随机推荐
- POI导出Execl文件,使JAVA虚拟机OOM
由于在项目中使用POI导出execl,导致JAVA虚拟机OOM,采用以下方式解决问题: 原先方式: g_wb = new XSSFWorkbook(sourceFile.getInputStream( ...
- Centos7更改yum源
每次都要百度一番,还不如自己做个记录,简单粗暴,哈哈哈哈 cd /etc/yum.repos.d mv CentOS-Base.repo CentOS-Base.repo.old wget http: ...
- 集群中节点(Node)与单机数据库的区别
集群中节点(Node)与单机数据库的区别: 区别项 集群中节点(Node) 单机数据库 只能使用0号数据库 是 都可以使用
- Spring生态研习【二】:SpEL(Spring Expression Language)
1. SpEL功能简介 它是spring生态里面的一个功能强大的描述语言,支在在运行期间对象图里面的数据查询和数据操作.语法和标准的EL一样,但是支持一些额外的功能特性,最显著的就是方法调用以及基本字 ...
- BVLC CaffeNet可视化及类别预测
一.介绍 bvlc_reference_caffenet网络模型是由AlexNet的网络模型改写的,输入图片尺寸大小为227x227x3,输出的为该图片对应1000个分类的概率值. 介绍参考:caff ...
- redis总结问题
简单回顾了redis,在这过程中 首先得了解redis是什么,redis的运用场景,redis支持哪些数据格式,redis如何操作数据,redis如何实现高可用 redis是什么: Redis 是一个 ...
- 如何判断ACCESS数据库有无密码
因为没有密码的数据库即使加上密码选项连接也不报错,所以如果通过连接来判读就无法识别无密码的数据库. 通过设置密码可以来测试数据库是否有密码,这是由于修改数据库密码的前提是数据库必须先有密码才行,如果数 ...
- c# 数据结构 ArrayList
数据结构 描述数据之间的关系 行为:添加数据,删除数据,插入数据,查找数据,修改数据 追加数据:向这个结构的末尾添加一个数据 删除数据:在这个结构中删除你指定的数据 插入数据:向这个结构中某一个位置插 ...
- laydate.js在火狐下的定位问题
这个情况不知道在火狐的什么情况下会出现这个问题,但是他的demo定位在火狐下是没有问题的. 正常情况下展示位置是这样的 但是呢,在我的火狐下展示的位置是这样的. 哎,默默的读源码吧: 源码: 修改后的 ...
- ADO.NET链接数据库封装方法
/// <summary> /// 获取一个表 /// </summary> /// <param name="sql ...