博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 156: Binary Tree Upside Down
阅读量:5308 次
发布时间:2019-06-14

本文共 5785 字,大约阅读时间需要 19 分钟。

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  * Definition for a binary tree node.  3  * public class TreeNode {  4  *     public int val;  5  *     public TreeNode left;  6  *     public TreeNode right;  7  *     public TreeNode(int x) { val = x; }  8  * }  9  */ 10  11 // iterative solution with O(1) space 12 public class Solution { 13     public TreeNode UpsideDownBinaryTree(TreeNode root) { 14         if (root == null) return null; 15          16         TreeNode cur = root, parent = null, parentRight = null; 17          18         while (cur != null) 19         { 20             TreeNode curLeft = cur.left, curRight = cur.right; 21              22             cur.right = parent; 23             cur.left = parentRight; 24              25             parent = cur; 26             parentRight = curRight; 27             cur = curLeft; 28         } 29          30         return parent; 31     }  32 } 33  34 // recurive solution 35 public class Solution1 { 36     public TreeNode UpsideDownBinaryTree(TreeNode root) { 37         if (root == null) return null; 38         var newRoot = new TreeNode[1]; 39          40         DFS(root, newRoot); 41          42         return newRoot[0]; 43     }  44      45     private TreeNode DFS(TreeNode node, TreeNode[] newRoot) 46     { 47         if (node.left == null) 48         { 49             newRoot[0] = node; 50             return node; 51         } 52          53         var root = DFS(node.left, newRoot); 54          55         node.left = null; 56         var right = node.right; 57         node.right = null; 58          59         root.left = right; 60         root.right = node; 61          62         return node; 63     } 64 } 65  66 // cleaner recurive solution 67 public class Solution2 { 68     public TreeNode UpsideDownBinaryTree(TreeNode root) { 69         if (root == null || root.left == null) return root; 70          71         var newRoot = UpsideDownBinaryTree(root.left); 72          73         root.left.left = root.right; 74         root.left.right = root; 75          76         root.left = null; 77         root.right = null; 78          79         return newRoot; 80     }      81 } 82  83 // iterative solution 84 public class Solution3 { 85     public TreeNode UpsideDownBinaryTree(TreeNode root) { 86         if (root == null) return null; 87          88         var stack = new Stack
(); 89 90 while (root != null) 91 { 92 stack.Push(root); 93 root = root.left; 94 stack.Peek().left = null; 95 } 96 97 var newRoot = stack.Pop(); 98 var cur = newRoot; 99 100 while (stack.Count > 0)101 {102 var top = stack.Pop();103 cur.left = top.right;104 cur.right = top;105 top.right = null;106 cur = top;107 }108 109 return newRoot;110 } 111 }

 

 
1 /** 2  * Definition for a binary tree node. 3  * public class TreeNode { 4  *     public int val; 5  *     public TreeNode left; 6  *     public TreeNode right; 7  *     public TreeNode(int x) { val = x; } 8  * } 9  */10 11 // iterative solution with O(1) space12 public class Solution {13     public TreeNode UpsideDownBinaryTree(TreeNode root) {14         if (root == null) return null;15         16         TreeNode cur = root.left, parent = root, parentRight = root.right;17         root.left = null;18         root.right = null;19         20         while (cur != null)21         {22             TreeNode curLeft = cur.left, curRight = cur.right;23             24             cur.right = parent;25             cur.left = parentRight;26             27             parent = cur;28             parentRight = curRight;29             cur = curLeft;30         }31         32         return parent;33     } 34 }35 36 // recurive solution37 public class Solution1 {38     public TreeNode UpsideDownBinaryTree(TreeNode root) {39         if (root == null) return null;40         var newRoot = new TreeNode[1];41         42         DFS(root, newRoot);43         44         return newRoot[0];45     } 46     47     private TreeNode DFS(TreeNode node, TreeNode[] newRoot)48     {49         if (node.left == null)50         {51             newRoot[0] = node;52             return node;53         }54         55         var root = DFS(node.left, newRoot);56         57         node.left = null;58         var right = node.right;59         node.right = null;60         61         root.left = right;62         root.right = node;63         64         return node;65     }66 }67 68 // iterative solution69 public class Solution2 {70     public TreeNode UpsideDownBinaryTree(TreeNode root) {71         if (root == null) return null;72         73         var stack = new Stack
();74 75 while (root != null)76 {77 stack.Push(root);78 root = root.left;79 stack.Peek().left = null;80 }81 82 var newRoot = stack.Pop();83 var cur = newRoot;84 85 while (stack.Count > 0)86 {87 var top = stack.Pop();88 cur.left = top.right;89 cur.right = top;90 top.right = null;91 cur = top;92 }93 94 return newRoot;95 } 96 }

 

转载于:https://www.cnblogs.com/liangmou/p/8055894.html

你可能感兴趣的文章
spoj TBATTLE 质因数分解+二分
查看>>
Django 模型层
查看>>
dedecms讲解-arc.listview.class.php分析,列表页展示
查看>>
Extjs6 经典版 combo下拉框数据的使用及动态传参
查看>>
【NodeJS】http-server.cmd
查看>>
研磨JavaScript系列(五):奇妙的对象
查看>>
面试题2
查看>>
selenium+java iframe定位
查看>>
P2P综述
查看>>
第五章 如何使用Burp Target
查看>>
Sprint阶段测试评分总结
查看>>
sqlite3经常使用命令&语法
查看>>
linux下编译openjdk8
查看>>
【python】--迭代器生成器装饰器
查看>>
Pow(x, n)
查看>>
安卓当中的线程和每秒刷一次
查看>>
每日一库:Modernizr.js,es5-shim.js,es5-safe.js
查看>>
ajax连接服务器框架
查看>>
wpf样式绑定 行为绑定 事件关联 路由事件实例
查看>>
利用maven管理项目之POM文件配置
查看>>