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.

Example:

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

Clarification:

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

这道题让我们把一棵二叉树上下颠倒一下,而且限制了右节点要么为空要么一定会有对应的左节点。上下颠倒后原来二叉树的最左子节点变成了根节点,其对应的右节点变成了其左子节点,其父节点变成了其右子节点,相当于顺时针旋转了一下。对于一般树的题都会有迭代和递归两种解法,这道题也不例外,先来看看递归的解法。对于一个根节点来说,目标是将其左子节点变为根节点,右子节点变为左子节点,原根节点变为右子节点,首先判断这个根节点是否存在,且其有没有左子节点,如果不满足这两个条件的话,直接返回即可,不需要翻转操作。那么不停的对左子节点调用递归函数,直到到达最左子节点开始翻转,翻转好最左子节点后,开始回到上一个左子节点继续翻转即可,直至翻转完整棵树,参见代码如下:

解法一:

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;
}
};

下面我们来看迭代的方法,和递归方法相反的时,这个是从上往下开始翻转,直至翻转到最左子节点,参见代码如下:

解法二:

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;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/156

类似题目:

Reverse Linked List

参考资料:

https://leetcode.com/problems/binary-tree-upside-down/

https://leetcode.com/problems/binary-tree-upside-down/discuss/49412/Clean-Java-solution

https://leetcode.com/problems/binary-tree-upside-down/discuss/49432/Easy-O(n)-iteration-solution-Java

https://leetcode.com/problems/binary-tree-upside-down/discuss/49406/Java-recursive-(O(logn)-space)-and-iterative-solutions-(O(1)-space)-with-explanation-and-figure

LeetCode All in One 题目讲解汇总(持续更新中...)

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

  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] 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. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

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

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

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

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

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

  7. LeetCode 563. Binary Tree Tilt (二叉树的倾斜度)

    Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...

  8. LeetCode 257. Binary Tree Paths (二叉树路径)

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  9. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

随机推荐

  1. 在.net中读写config文件的各种方法【转】

    今天谈谈在.net中读写config文件的各种方法. 在这篇博客中,我将介绍各种配置文件的读写操作. 由于内容较为直观,因此没有过多的空道理,只有实实在在的演示代码, 目的只为了再现实战开发中的各种场 ...

  2. Python3.0的新特性

    网上关于Python3与Python2的区别的文章都烂大街了,但基本上都是抄来抄去,为了追本溯源,直接看官网最靠谱,官网文档的结构性更强. 本文是对Python3.0官网文档 What's New I ...

  3. A Pattern Language for Parallel Application Programming

    A Pattern Language for Parallel Application Programming Berna L. Massingill, Timothy G. Mattson, Bev ...

  4. RFC的远程调用-异步

    接上篇RFC的远程调用-同步(https://www.cnblogs.com/BruceKing/p/11169930.html). TABLES:USR21. DATA:A TYPE USR21-P ...

  5. WPF 选择文件选择文件夹

    namespace Microsoft.Win32 选择文件: if (string.IsNullOrEmpty(folderInitialDirectory)) { folderInitialDir ...

  6. NET Excel转换为集合对象

    1.仅适用于规则Excel:表头和数据一一对应 2.涉及到Excel转换为集合对象的部分代码,完整npoi帮助类点击查看 /// <summary> /// 默认把excel第一个shee ...

  7. Winform中设置ZedGraph的颜色填充使用Fill

    场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  8. STorM32 BGC三轴云台控制板电机驱动电路设计(驱动芯片DRV8313)

    1  序言 相信对云台有兴趣的小伙伴对STorM32 BGC这块云台控制板并不陌生,虽说这块控制板的软件已经不再开源,但是在GitHub上依旧可以找到两三个版本的代码,而硬件呢我们也可以从Olliw( ...

  9. 如何搭建wordpress ,wecenter

    14.什么是LNMP架构 LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写.L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可 ...

  10. ObjectARX之Mac os开发

    网上关于ObjectARX开发的相关介绍都基于Windows环境.Mac 环境的开发几乎没有,下面介绍Mac环境下ObjectARX的开发. AutoCAD ObjectARX开发必备条件: 1. 安 ...