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 ...
随机推荐
- django admin 支持时间更改
from django.db import models import django.utils.timezone as timezone class Doc(models.Model): add_d ...
- PythonStudy——赋值运算符 Assignment operator
eg: num = 10 num += 1 # 等价于 num = num + 1 => 11 print(num) 特殊操作: 1.链式赋值 a = b = num print(a, b, n ...
- MySQL Error--Error Code
mysql error code(备忘) 1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导 ...
- JVM的基本结构及其各部分详解(二)
3.2 栈帧组成之操作数栈 操作数栈是栈帧的主要内容之一,它主要用于保存计算过程中的中间结果,同时作为计算过程中变量临时的存储空间. 操作数栈也是一个先进后出的数据结构,只支持入栈和出栈两种操作,许多 ...
- Git和Svn对比
From: https://wenku.baidu.com/view/1f090e2e7275a417866fb84ae45c3b3567ecdd12.html Git和Svn对比 共享文档 ...
- python修改字典的值(update map value)
mydict.update({'newkey':'newvalue'})
- 用网站把图标做成iconFont文件引用
1,把psd文件另存为eps文件(ai能打开的格式),前提图标有路径, 2,用ai打开eps文件 3,新建一个空白文件100*100,然后把图标复制进来,等比拉宽至最大化 4,如果图标有蒙版,就点击图 ...
- Pyspider框架
1, 2,在ubuntu安装pyspider如果出现pycul的问题 首先执行命令:sudo apt-get install libssl-dev libcurl4-openssl-dev pytho ...
- 详解vue-cli脚手架项目-package.json
该随笔收藏自: 详解vue-cli脚手架项目-package.json package.json是npm的配置文件,里面设定了脚本以及项目依赖的库. npm run dev 这样的命令就写在packa ...
- python-web自动化-三种等待方式
当有元素定位不到时,比如下拉框,弹出框等各种定位不到时:一般是两种问题:1 .有frame :2.没有加等待 下面学习三种等待方式: 1.强制等待 sleep(xx)这种方法简单粗暴,不管浏览器是否加 ...