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.

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

Confused what [4,5,2,#,#,3,1] means? Read more below on how binary tree is serialized on OJ.

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
/ \
2 3
/
4
\
5

The above binary tree is serialized as [1,2,3,#,#,4,#,#,5].

题意:

将一棵树,按照一种方式上下颠倒

Solution1: Tree + Recursion

Need save the tree information before changing the tree structure

Now, the basic idea is to go though from the original root (2), make root’s left child(4)  as newParent whose left child will be the original root’s right child (5) and right child will be the original root (2). After that, you set new root to root.left (5), and process it in the same way.

代码:

 class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if(root == null || root.left == null) return root;
TreeNode newNode = upsideDownBinaryTree(root.left); // newNode最后要返回,所以找到、存下来后,就不再动了
root.left.left = root.right;// 这里为何不是newNode.left = root.left ?
root.left.right = root;
root.left = null ;
root.right = null;
return newNode;
}
}

[leetcode]156.Binary Tree Upside Down颠倒二叉树的更多相关文章

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

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

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

  3. 156. Binary Tree Upside Down反转二叉树

    [抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...

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

  5. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  6. [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  7. [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  8. 【LeetCode】Binary Tree Upside Down

    Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a s ...

  9. [LeetCode] 152. Binary Tree Upside Down 二叉树的上下颠倒

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

随机推荐

  1. 安卓控制LED驱动编写

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 打开Android Stud ...

  2. 【转载】Win10系统桌面右键新建没有Word、Excel、PPT怎么恢复?

    Win10系统桌面右键新建没有Word.Excel.PPT怎么恢复? 以下正文转载至: 网址:http://www.xitongzhijia.net/xtjc/20170307/93471.html ...

  3. 虚拟化cpu

    vmware的虚拟机cpu [root@84-monitor ~]# lscpuArchitecture:          x86_64CPU op-mode(s):        32-bit, ...

  4. MySQL 中,字符串 0 和数字 0 的区别

    我的理解: 用户输入值后,MySQL 根据该字段的数据类型,来转换值.

  5. Matlab关于视觉问题中的一些自有API

    [randsample/randperm] y = randsample(n,k);从1:n中随机抽取k个数. y=  randperm(n)或者y=  randperm(n,k) [rectint] ...

  6. centos 使用RPM包安装指定版本的docker-engine

    下面是拿安装docker-engine-1.10.3-1为例: wget https://yum.dockerproject.org/repo/main/centos/7/Packages/docke ...

  7. Docker基本命令与使用 —— Docker容器的网络连接(四)

    一.Docker容器的网络基础 通过ifconfig查看docker0的网络设备,docker守护进程就是通过docker0为docker的容器提供网络连接的各种服务. docker0是Linux虚拟 ...

  8. Node学习笔记(一)

    1. node的特点: Node.js 不是一种独立的语言,与 PHP.Python.Perl.Ruby 的“既是语言也是平台”不同.Node.js 也不是一个 JavaScript 框架,不同于 C ...

  9. linux git clone 指定分支

    git clone -b develop http://192.168.11.11:8888/scm/git/vrmmo 指定下载develop分支

  10. 在C++程序中自动加入svn版本号

    原创文章,欢迎阅读,如果您想转载,请在第一行醒目注明原作者和原始链接. 为了方便追查和确认软件bug等问题,给软件或者库赋予版本号是个好办法. 最简单的版本号管理是记录编译时间: cout<&l ...