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. MVC 模式

    1.MVC 模式简介 MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式.这种模式用于应用程序的分层开发.Model(模型):模型代表一个存取数据的对象或 JAV ...

  2. Ubuntu 16.04出现:Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/cache/app-info -a -e /usr/bin/appstreamcli; then appstreamcli refresh > /dev/null; fi'

    错误: Reading package lists... Done E: Problem executing scripts APT::Update::Post-Invoke-Success 'if ...

  3. InnoDB存储引擎文件

    InnoDB存储引擎文件 MySQL数据库包括数据库本身的文件和存储引擎文件.数据库自身的文件由参数文件(my.cnf).错误日志文件.慢查询日志文件.查询日志文件.二进制日志文件.套接字文件.pid ...

  4. Mysql索引会失效的几种情况

    1.如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因): 2.对于多列索引,不是使用的第一部分,则不会使用索引: 3.like查询是以%开头: 4.如果列类型是字符串, ...

  5. linux--切换ipython解释器到python3

    Ipython修改为python3解释器: which ipython --得到路径 cat 路径--查看执行的解释器版本 sudo gedit 路径--修改解释器版本为python3 保存即可,保存 ...

  6. springboot对oracle的配置

    spring.jpa.database=oracle spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver sprin ...

  7. python eval()和exec()以及complie()

    1.eval() 函数 eval() 函数用来执行一个字符串表达式,并返回表达式的值. ------->>  eval(expression[, globals[, locals]]) 参 ...

  8. 用PS做圆角图片

    ps: Adobe Photoshop CS2  如果图片被锁定,请“双击”图层中“背景”解锁,如果没有图层菜单,在最上面导航栏中:窗口—图层.如下图:  点“确定”,解锁.  选用“圆角矩形工具”. ...

  9. Vue 父组件调用子组件函数的方法

    parent.vue(父组件的内容): <template> <div @click="divClick"> <info-wnd ref=" ...

  10. python3 kmp 字符串匹配

    先声明,本人菜鸟一个,写博客是为了记录学习的过程,以及自己的理解和心得,可能有的地方写的不好,希望大神指出... 抛出问题 给定一个文本串test_str(被匹配的字符串)和模式串pat_str(需要 ...