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

  

最新文章

  1. hdu 4025 2011上海赛区网络赛E 压缩 ***
  2. Nginx 中 fastcgi_pass 监听端口 unix socket和tcp socket差
  3. JS比较好用的一些方法搜集
  4. Ubuntu输入密码之后,桌面闪一下黑屏,然后又返回到输入密码界面。但是其他账户可以登入
  5. Oracle11.2.0.4 RAC安装文档
  6. 转:angular的decorator方法
  7. CentOS 7安装Teamviewer 12
  8. python之~ 序列化与反序列化
  9. 【java】基础中的杂乱总结(一)
  10. css清除浮动大全,共8种方法
  11. Swift开发教程--使用Storyboard进行界面跳转
  12. 任务调度--spring下的任务调度quartz
  13. pyspider安装提示:got an unexpected keyword argument 'io_loop'的解决办法
  14. js中一个对象中遇到一个相同的key所对应的value值相加
  15. SSE图像算法优化系列二十四: 基于形态学的图像后期抗锯齿算法--MLAA优化研究。
  16. 洗礼灵魂,修炼python(20)--自定义函数(1)—基础概念
  17. C语言四舍五入
  18. sap component 中各个组件的关系
  19. LeetCode - Rotate String
  20. YII2开启路由配置后,新加的模块无法访问

热门文章

  1. php string常用函数
  2. C# List<T>排序总结
  3. postgresql 导入 导出(一张表)
  4. c#中的继承学习总结
  5. tensorflow API _ 3 (tf.train.polynomial_decay)
  6. 国赛baby_pwn
  7. springboot的HTTPS配置
  8. ssh2
  9. windbg在加载模块时下断点
  10. Thread线程框架学习