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 ...
随机推荐
- BEAM188和LINK180简单实例
简介 一开始想做一个绳索单元悬挂重物的仿真,其实想法很简单,但是在实现过程中却出现了很大的问题,纠结了很久,初步归结为:绳索单元在垂直其单元方向上受力,会导致其产生很大的变形,从而导致其不收敛.因此专 ...
- ScriptManager 发送错误到客户端
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- 添加一个pv到vg后,误删新加入的pv,报 pv * not found or rejected by a filter
问题如下 将某一pv加入vg vgextend cl /dev/xvdb1 然后进入fdisk将xvdb1分区删掉,重新创建pv 使用lvdisplay报警告 [root@localhost ~]# ...
- Maya中提交Nuke工程到deadline中的方法
在之前的一篇文中介绍了在maya中生成nuke工程脚本的方法,后来部门负责人希望更简单一些,能在那个功能面板里提交deadline农场渲染更好,这样就不用打开nuke手动提交了,省去了在两个软件直接来 ...
- bcdboot(引导修复工具) 命令行工具使用方法
BCDboot 是一种用于快速设置系统分区或修复系统分区上的启动环境的工具.系统分区是通过从已安装的 Windows(R) 映像复制一小部分启动环境文件来设置的.BCDboot 还会在系统分区上创建引 ...
- Kong管理UI -kong-dashboard
本文仍然是在ubuntu18的环境下进行 https://github.com/PGBI/kong-dashboard kong dashboart如果要正常使用管理UI,前提为kong已经正常run ...
- ss客户端以及tcp,udp,dns代理ss-tproxy在线安装版--centos7.3 x64以上(7.3-7.6x64测试通过)
#!/bin/sh # # Script for automatic setup of an SS-TPROXY server on CentOS 7.3 Minimal. # export PATH ...
- jvm--深入理解java虚拟机 精华总结(面试)(转)
深入理解java虚拟机 精华总结(面试)(转) 原文地址:http://www.cnblogs.com/prayers/p/5515245.html 一.运行时数据区域 3 1.1 程序计数器 3 1 ...
- Jobs深入学习
代码回顾 Quartz 需要了解你可能希望该作业的实例拥有的各种属性,这是通过JobDetail 类完成的. JobDetail 实例是使用 JobBuilder 类构建的. JobDetail j ...
- [java,2017-05-16] java中清空StringBuffer的方法以及耗费时间比较
java中清空StringBuffer的方法,我能想到的有4种: 1. buffer.setLength(0); 设置长度为0 2. buffer.delete(0, buffer.length() ...