[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 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 二叉树的上下颠倒的更多相关文章
- [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 ...
- ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java
156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions Total Accepted: ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【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 ...
- 156. Binary Tree Upside Down反转二叉树
[抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...
- 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
随机推荐
- 【转】GnuPG使用介绍
一.什么是 GPG 要了解什么是 GPG,就要先了解 PGP. 1991 年,程序员 Phil Zimmermann 为了避开政府监视,开发了加密软件 PGP.这个软件非常好用,迅速流传开来,成了许多 ...
- strcspn函数的用法
#include <string.h> main() { char *str = "Linux was first developed for 386/486-based pcs ...
- Java 多线程实战
Java多线程 public class ThreadTest { public static void main(String[] args) throws InterruptedException ...
- 9、Hadoop配置文件和HDFS垃圾回收
配置文件 默认配置文件:相对应的jar包中 core-default.xml hdfs-default.xml yarn-default.xml mapred-default.xml 自定义配置文件 ...
- 001_Visual Studio 显示数组波形
视频教程:https://v.qq.com/x/page/z3039pr02eh.html 资料下载:https://download.csdn.net/download/xiaoguoge11/12 ...
- c++中 string类型 转为 char []类型
将string类型转换为字符数组char [] char arr[50]; //数组大小根据s的大小确定 string s= "12slfjksldkfjlsfk"; int le ...
- UFUN函数 UF_ATTR函数(UF_ATTR_assign ,UF_ATTR_read_value )
UF_initialize(); tag_t ; ]="零件名称"; UF_ATTR_value_t value; value.type=UF_ATTR_string; value ...
- 虚拟机Linux系统ip查询失败问题
当用SSH连接Linux需要ip地址,但是不论是通过ipconfig命令,还是通过ip addr命令都无法获取Linux的ip,通过以下方法成功解决了该问题: 1.点击编辑里面的虚拟网络编辑器出现如下 ...
- lyft amundsen简单试用
昨天有说过amundsen 官方为我们提供了dockerc-compose 运行的参考配置,以下是一个来自官方的 quick start clone amundsen 代码 amundsen 使用了g ...
- C# 监测每个方法的执行次数和占用时间(测试4)
今天也要做这个功能,就百度一下,结果搜索到了自己的文章.一开始还没注意,当看到里面的一个注释的方法时,一开始还以为自己复制错了代码,结果仔细一看网页的文章,我去,原来是自己写的,写的确实不咋地. 把自 ...