LeetCode Binary Tree Upside Down
原题链接在这里:https://leetcode.com/problems/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
题解:
Recursion 方法是自底向上.
Time Complexity: O(n).
Space: O(n). tree height.
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if(root == null || root.left == null){
return root;
}
TreeNode newRoot = upsideDownBinaryTree(root.left); root.left.left = root.right;
root.left.right = root; root.left = null;
root.right = null;
return newRoot;
}
}
Iterative 是从上到下.
Time Complexity: O(n). Space: O(1).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if(root == null || root.left == null){
return root;
}
TreeNode cur = root;
TreeNode next = null;
TreeNode pre = null;
TreeNode temp = null;
while(cur != null){
next = cur.left;
cur.left = temp;
temp = cur.right;
cur.right = pre;
pre = cur;
cur = next;
}
return pre;
}
}
LeetCode Binary Tree Upside Down的更多相关文章
- [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 旋转树 --------- java
156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions Total Accepted: ...
- 【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 ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- [Locked] Binary Tree Upside Down
Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a s ...
- [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
Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left ...
- [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 ...
随机推荐
- Scala _ [underscore] magic
I started learning Scala a few days before. Initially i was annoyed by the use of too many symbols i ...
- java中.equals和==的区别?
Java中的equals是十分重要的,和= =要区别开来,孙卫琴的JAVA面向对象编程一书对这个做了阐述,现在小结其主要内容,而且要将 = =和 equals列为重要的对比概念来学习 1.声明格式 ...
- Sql Group by 使用
CREATE TABLE StuCourseScore ( ID int, Name nvarchar(10), Course nvarchar(10), Score int ) INSERT Stu ...
- github 向导/介绍
环境:windows 7 64bit 阅读了http://guides.github.com的教程. 流程 git是协作版本管理的一种方法.工作流程是在一个主线基础上发展分支(branch),最后并入 ...
- PHP 设计模式 笔记与总结(8)策略模式
① 策略模式,将一组特定的行为和算法封装成类,以适应某些特定的上下文环境,这种模式就是策略模式. ② 实际应用举例,假如一个电商网站系统,针对男性女性用户要各自跳转到不同的商品类目,并且所有广告位展示 ...
- 循环btn上面的视图
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- FAQ软件卸载
今天安装了一个PDF 编辑软件pdftk,下载时提示有50多M,感觉不好用,卸载后提示释放空间只有2M,郁闷了.上网查寻,如下命令 sudo dpkg -p package_name卸载软件包及其配置 ...
- Unity3d中C#使用指针(Unsafe)的办法(转)
近日由于在U3D项目中要使用到数据传递(C++ DLL的数据传递给U3D中的C#),其中涉及到需要使用C#的指针.直接编译会出现以下错误Unsafe code requires the 'unsafe ...
- 为什么数值类型byte取值范围是(-128~127)?
在解决这个问题之前,我们先了解几个概念? 一.原码, 反码, 补码的概念 正数的反码和补码都与原码一样: 负数的反码.补码与原码不同,负数的反码:原码中除去符号位,其他的数值位取反,0变1,1变0.负 ...
- Natural Language Toolkit
http://www.nltk.org/ >>> import nltk >>> nltk.download()