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 ...
随机推荐
- Mac or Centos 下如何编译objective-C
#import <Foundation/Foundation.h> int main(int argc,const char *argv[]){ @autoreleasepool{ NSL ...
- (转载)c# winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值
第一种方法: 用委托,Form2和Form3是同一组 Form2 C#代码 using System; using System.Collections.Generic; using System.C ...
- 新鲜出炉的30个精美的 jQuery & CSS3 效果【附演示和教程】
新鲜出炉的30个精美的 jQuery & CSS3 效果[附演示和教程] 作为最流行的 JavaScript 开发框架,jQuery 在现在的 Web 开发项目中扮演着重要角色,它简化了 ...
- 3. Configure the Identity Service
Controller Node: 安装认证服务: 1. sudo apt-get install keystone 2. sudo vi /etc/keystone/keystone.conf [ ...
- 获取某个Group中所有对象的DisplayName
$SANs = Get-ADGroupMember -Identity "CN=gAPCHN-HGZ-IE10-Users,OU=Groups,OU=Hangzhou - China,OU= ...
- eclipse远程调试
-Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000Eclipse 菜单上的 Window > Preferences > ...
- CSS3系列:流式(弹性)布局(flex布局)
我的新伸缩盒子.http://www.cnblogs.com/leee/p/5533436.html
- 【ArcGis for javascript从零开始】之一 ArcGis加载天地图
最近做项目需要用到ArcGis来进行数据展示和数据分析.以前从来没有接触过与Gis有关的东西,一切需要从头开始学.没有时间从头系统地学习了,只能用到哪个学习哪里了,本系列只是对学习的路径进行记录.Ar ...
- ptmalloc2源码解析初探
本文是徽沪一郞在学习华庭(庄明强)所撰<glibc内存管理-ptmalloc2源代码分析>的阅读笔记.本笔记以slides的方式加以呈现.文件采用latex+tikz编辑而成,如果对lat ...
- Mininet实验 基于Mininet实现BGP路径挟持攻击实验
参考:基于Mininet实现BGP路径挟持攻击实验 实验目的: 掌握如何mininet内模拟AS. 掌握BGP路径挟持的原理和分析过程. 实验原理: 互联网是由相互连接的自治系统AS组成的,通过一个通 ...