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.

For example:
Given a binary tree {1,2,3,4,5},

    1
/ \
2 3
/ \
4 5

return the root of the binary tree [4,5,2,#,#,3,1].

   4
/ \
5 2
/ \
3 1

分析:

对于树,递归总是是很好的实现方法,所以这题应该

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if (root == null || root.left == null) {
return root;
}
TreeNode newRoot = upsideDownBinaryTree(root.left); root.left.right = root;
root.left.left = root.right; root.left = null;
root.right = null; return newRoot;
}
}

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

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

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

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

  5. LeetCode Binary Tree Upside Down

    原题链接在这里:https://leetcode.com/problems/binary-tree-upside-down/ Given a binary tree where all the rig ...

  6. 156. Binary Tree Upside Down

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

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

  8. [Swift]LeetCode156.二叉树的上下颠倒 $ Binary Tree Upside Down

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

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

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

随机推荐

  1. JS原型链理解

    1. 每个对象都有原型属性(__proto__)2. 对象的原型(__proto__)指向其构造函数(Class)的prototype属性3. 构造函数(Class)的prototype属性本身也是一 ...

  2. QT笔记

    1.菜单栏上的弹出窗口 void MainWindow::on_new_action_triggered() {     MyDialog myDialog;//MyDialog是一个ui     m ...

  3. [Js/Jquery]table行转列

    摘要 在使用ews调用exhange的收件箱的并在h5页面显示邮件详情的时候,因为返回的每封邮件的内容都是htmlbody,没有textbody.每封邮件又没什么规律,用正则表达式来匹配内容并不合适, ...

  4. linux如何隐藏和显示所有窗口?

    centos7 基本上就跟fedora23是一样的了, 也许它们使用的内和是一样的, fedora23使用的是4.2.3, 所以使用fedora对使用centos和redhat是有优势和好处福利的. ...

  5. oracle vm virtualbox右ctrl切换显示模式

    转自: http://blog.csdn.net/lyc_daniel/article/details/44195515 virtualbox里面有个HOME键,注意这个HOME键不一定是键盘上的HO ...

  6. Android 开发

    1.Activity 子类会重载一个 protected void onCreate(Bundle savedInstanceState) { ... } 方法,其中 savedInstanceSta ...

  7. Android 环境配置

    一.开发环境配置 1.使用 eclipse 需要下载安装 Android SDK.Eclipse.ADT 插件. 也可以直接下载整合好的 ADT Bundle 包,下载地址: http://devel ...

  8. 响应性web设计实战总结(二)

    响应性web设计实战总结(二) 阅读目录 背景知识: Gulp-less安装及配置如下 对响应性web总结,之前总结过2篇文章:可以看如下: http://www.cnblogs.com/tugenh ...

  9. IIS 您要访问的网页有问题,无法显示!

    提示:您要访问的网页有问题,无法显示.HTTP 500 – 内部服务器错误.”的问题解决方案! IIS服务器出现错误的原因很多,请尝试以下操作:1.查看网站属性——文档看看启用默认文档中是否存在:in ...

  10. ASP数据库操作方法

    首先,必须要使用打开数据库方法: <% dim objconn,objconnstr set objconn=server.createobject("adodb.connection ...