[抄题]:

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反转二叉树的更多相关文章

  1. [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 ...

  2. ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java

    156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions   Total Accepted: ...

  3. [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 ...

  4. 156. Binary Tree Upside Down

    题目: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node ...

  5. 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. cordova日期插件的使用:cordova-plugin-datepicker

    1. 添加插件:cordova plugin add cordova-plugin-datepicker; 2.插件的主体样式设置: 3.以上5中样式的截图: THEME_TRADITIONAL的样式 ...

  2. 【SpringBoot】服务器端主动推送SSE技术讲解

    =====================16.高级篇幅之SpringBoot2.0服务器端主动推送SSE技术讲解 ============================ 1.服务端推送常用技术介绍 ...

  3. AR外包,就找北京动点软件,长年承接AR外包(案例丰富可签合同,内附案例演示)

    北京团队长年承接AR项目外包 咨询QQ:372900288  微信:liuxiang0884 以下是AR项目案例演示,索取更多案例请通过以上方式在线联系我们

  4. Wireshark win7 没有找到接口;找不到接口

    下载安装winpcap: https://www.winpcap.org/install/default.htm

  5. [UE4]Native Widget Host

    一.Native Widget Host是一个容器,它可以包含一个Slate UI 二.Native Widget Host应该用在当你需要把一个Slate UI 放到UMG中的时候,只有这个时候才需 ...

  6. Northwind数据库练习及参考答案

    --查询订购日期在1996年7月1日至1996年7月15日之间的订单的订购日期.订单ID.客户ID和雇员ID等字段的值 Create View Orderquery as Select OrderDa ...

  7. 如何使用jQuery从字符串中删除最后一个字符

    如何使用jQuery从字符串中删除最后一个字符 1.string.slice(0,-1) 2.str.substring(0,str.length-1)

  8. node.js打印function

    var Person = function(name) { this.name = name; this.gender = ['man', 'woman']; } console.log(Person ...

  9. git中提交了想要忽略的文件,如何在删除

    我们在用git的时候,有时会不小心将不需要文件跟踪的文件(如.classpath文件.project等)提交到git的服务器,这时候要忽略这些文件的做法是: 1.修改.gitignore文件 按照规则 ...

  10. IDEA jrebet插件安装

    破解.exe下载 https://github.com/ilanyu/ReverseProxy/releases/tag/v1.0 双击运行,exe 文件, 然后IDEA -> Help -&g ...