156. Binary Tree Upside Down

Add to List
QuestionEditorial Solution

My Submissions

 
  • Total Accepted: 18225
  • Total Submissions: 43407
  • Difficulty: Medium
  • Contributors: Admin

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

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

OJ's Binary Tree Serialization:

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}".

 
给定一棵树,这棵树的右节点有两种选择:1、空节点 2、叶子结点(左节点一定存在)
 
然后旋转该树(结构对称)且左节点变换为:1、空节点 2、叶子结点(右节点一定存在)
 
那么就可以用递归和非递归两种形式实现。
 
我选择了非递归的实现方法。
 
自顶向下:给定[1,2,3],变换为[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 && root.right == null)){
return root;
}
TreeNode node = root;
TreeNode nodeLeft = root.left;
TreeNode nodeRight = root.right;
while (nodeLeft != null){
TreeNode nodeChange = node;
TreeNode nodeLeftChange = nodeLeft;
TreeNode nodeRightChange = nodeRight;
node = nodeLeft;
nodeLeft = node.left;
nodeRight = node.right;
nodeLeftChange.left = nodeRightChange;
nodeLeftChange.right = nodeChange;
}
root.left = null;
root.right = null;
return node;
}
}
 
 

✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java的更多相关文章

  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

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

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

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

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

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

  7. 156. Binary Tree Upside Down

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

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

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

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

随机推荐

  1. Hibernate 通过 Session 操纵对象

    Session 概述 •Session 接口是 Hibernate 向应用程序提供的操纵数据库的最主要的接口, 它提供了基本的保存, 更新, 删除和加载 Java 对象的方法. •Session 具有 ...

  2. Highcharts导出gb2312乱码问题

    Highcharts是utf-8编码的,其本地的.net导出环境也是utf-8格式的,导致网页如果采用gb2312编码,显示正常,导出就乱码了.这种现象也同样经常出现在ajax的使用过程中. ajax ...

  3. 支持单色条码图像生成的条形码控件Barcode Professional

    Barcode Professional for .NET Windows Forms条形码控件是一款灵活和强大的.NET组件(.NET DLL 类库),它让您轻松地添加条码生成和打印功能到您的.NE ...

  4. SharePoint 2013 重复的管理账户错误:已添加项。字典中的关键字 所添加的关键字

    博客地址:http://blog.csdn.net/FoxDave 今天在管理中心创建新的Web应用程序时,想注册一个新的管理账户,一着急点了两次按钮,结果就出现了这样的错误...怎么说呢,太奇葩 ...

  5. iOS开发之UITableView使用总结

    什么是UITableView 在众多移动应用中,能看到各式各样的表格数据 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView UITableView继承自UIScrollVie ...

  6. adaboost算法

    三 Adaboost 算法 AdaBoost 是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器,即弱分类器,然后把这些弱分类器集合起来,构造一个更强的最终分类器.(很多博客里说的三个臭皮匠 ...

  7. Spring中@Resource、@controller注解的含义

    @Resource 注解被用来激活一个命名资源(named resource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI context中的一个对象. Spring确实支 ...

  8. iOS 高级框架的介绍

    Core Data.Core Graphics.Core Animation 和 OpenGL ES 框架,是对于应用程序开发很重要的高级技术,因此需要花时间来学习和掌握. Core Data 框架管 ...

  9. 64位Ubuntu下的Eclipse、ADT终于可以生成R.java了,虚机也可以正常建立

    64位Ubuntu12下的Eclipse总也不能自动生成R.java,导致无法正常编译程序,建虚拟器的时候总是提示少文件....三天下班机器没关(无奈公司网络不给力)来安装lib包. 各种加载,总结一 ...

  10. 前序/中序--->后序

    参考:http://www.cnblogs.com/rain-lei/p/3576796.html !!由前序和后序序列无法确定二叉树 preOrder 5 3 2 4 8 6 9   midOrde ...