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

给一个二叉树,右节点要么为空要么一定会有对应的左节点,把二叉树上下颠倒一下,原二叉树的最左子节点变成了根节点,其对应的右节点变成了其左子节点,其父节点变成了其右子节点。

解法1:递归

解法2:迭代

Java: Time: O(N), Space: O(N)

public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if(root == null || root.left == null)return root;
TreeNode newRoot = upsideDownBinaryTree(root.left);
//root.left is newRoot everytime
root.left.left = root.right;
root.left.right = root;
root.left = null;
root.right = null;
return newRoot;
}
}

Java: Time: O(N), Space: O(1)

public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
TreeNode cur = root;
TreeNode pre = null;
TreeNode tmp = null;
TreeNode next = null;
while(cur != null){
next = cur.left;
//need tmp to keep the previous right child
cur.left = tmp;
tmp = cur.right; cur.right = pre;
pre = cur;
cur = next;
}
return pre;
}
}  

Python:

# Time:  O(n)
# Space: O(n)
class Solution2(object):
# @param root, a tree node
# @return root of the upside down tree
def upsideDownBinaryTree(self, root):
return self.upsideDownBinaryTreeRecu(root, None) def upsideDownBinaryTreeRecu(self, p, parent):
if p is None:
return parent root = self.upsideDownBinaryTreeRecu(p.left, p)
if parent:
p.left = parent.right
else:
p.left = None
p.right = parent return root

Python:

class Solution(object):
# @param root, a tree node
# @return root of the upside down tree
def upsideDownBinaryTree(self, root):
p, parent, parent_right = root, None, None while p:
left = p.left
p.left = parent_right
parent_right = p.right
p.right = parent
parent = p
p = left return parent  

C++:

// Recursion
class Solution {
public:
TreeNode *upsideDownBinaryTree(TreeNode *root) {
if (!root || !root->left) return root;
TreeNode *l = root->left, *r = root->right;
TreeNode *res = upsideDownBinaryTree(l);
l->left = r;
l->right = root;
root->left = NULL;
root->right = NULL;
return res;
}
}; 

C++:

// Iterative
class Solution {
public:
TreeNode *upsideDownBinaryTree(TreeNode *root) {
TreeNode *cur = root, *pre = NULL, *next = NULL, *tmp = NULL;
while (cur) {
next = cur->left;
cur->left = tmp;
tmp = cur->right;
cur->right = pre;
pre = cur;
cur = next;
}
return pre;
}
};

  

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

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

  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. [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#156] Binary Tree Upside Down

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

  6. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

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

  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. 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)

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

随机推荐

  1. 用IntelliJ IDEA学习Spring--创建一个简单的项目

    这段时间想学习一下Spring,其实之前学过Spring,只是有些忘记了.而且之前学的时候是适用eclipse学习的,现在好像对IntelliJ这个工具使用挺多的,现在就学习一下这个工具的用法,顺便复 ...

  2. Jquery的$(document).click() 在iphone手机上失效的问题

    click事件和 touchstart事件共存 安卓IOS手机都适用 $(document).on("click touchstart", ".demo", f ...

  3. PHP CGI 进程占用CPU过高导致CPU使用达到100%的另类原因

    由于使用的华为云的CDN加速,结果发现我的阿里云服务器突然卡顿,网页打开极慢.登陆华为云CDN管理后台发现最高带宽占用30M,流量短时间内达到10GB以上,这么大的流量我的服务器肯定扛不住啊.于是还跟 ...

  4. MapReduce如何解决数据倾斜?

    数据倾斜是日常大数据查询中隐形的一个BUG,遇不到它时你觉得数据倾斜也就是书本博客上的一个无病呻吟的偶然案例,但当你遇到它是你就会懊悔当初怎么不多了解一下这个赫赫有名的事故. https://www. ...

  5. django-改写manage类-objects

    user/models.py中 class AddressManage(models.Manager): '''地址模型管理类''' def get_default_addr(self, user): ...

  6. Haskell语言学习笔记(94)Enum Bounded

    Enum class Enum a where succ, pred :: a -> a toEnum :: Int -> a fromEnum :: a -> Int enumFr ...

  7. IGC(Interleaved Group Convolutions)

    深度学习被引起关注是在2012年,用神经网络训练的一个分类模型在ImagNet上取得了第一名,而且其分类精度比第二名高出10多个点,当时所使用的模型为AlexNet,现在看来其为一个比较简单的网络,而 ...

  8. MySQL 详细解读undo log :insert undo,update undo

    转自aobao.org/monthly/2015/04/01/ 本文是对整个Undo生命周期过程的阐述,代码分析基于当前最新的MySQL5.7版本.本文也可以作为了解整个Undo模块的代码导读.由于涉 ...

  9. Lightning Web Components 来自salesforce 的web 组件化解决方案

    Lightning Web Components 是一个轻量,快速,企业级别的web 组件化解决方案,官方网站也提供了很全的文档 对于我们学习使用还是很方便的,同时我们也可以方便的学习了解salesf ...

  10. Brexit Gym - 101490C

    题目链接:Brexit vector的使用(vector存边),巧用queue,相当于Bfs /* */ # include <iostream> # include <cstdio ...