LeetCode 156. Binary Tree Upside Down(树的倒转)
来源:互联网 发布:方正兰亭淘宝买了吗 编辑:程序博客网 时间:2024/06/06 02:24
原题网址: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
方法:递归。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { // 这个参数表示未来,未来的root,未来的left,未来的right,未来的flip函数啊,请帮我反转吧!!! private TreeNode flip(TreeNode fRoot, TreeNode fLeft, TreeNode fRight) { // 我现在就帮你反转,但请容我先记住必要的左子树 TreeNode fl = fRoot.left; TreeNode fr = fRoot.right; fRoot.left = fLeft; fRoot.right = fRight; if (fl == null) return fRoot; return flip(fl, fr, fRoot); } public TreeNode upsideDownBinaryTree(TreeNode root) { if (root == null || root.left == null) return root; TreeNode fl = root.left; TreeNode fr = root.right; root.left = null; root.right = null; return flip(fl, fr, root); }}
0 0
- LeetCode 156. Binary Tree Upside Down(树的倒转)
- leetcode-156.Binary Tree Upside Down
- [Leetcode]156.Binary Tree Upside Down
- Leetcode 156. Binary Tree Upside Down
- LeetCode—156. Binary Tree Upside Down
- LeetCode 156. Binary Tree Upside Down
- [Leetcode] Binary Tree Upside Down
- LeetCode Binary Tree Upside Down
- leetcode Binary Tree Upside Down
- [Leetcode] Binary Tree Upside Down
- leetcode--Binary Tree Upside Down
- LeetCode-Binary Tree Upside Down
- Leetcode Binary Tree Upside Down
- leetcode--Binary Tree Upside Down
- 156. Binary Tree Upside Down
- 156. Binary Tree Upside Down
- [LeetCode] Binary Tree Upside Down的三种解法
- [leetcode] 156. Binary Tree Upside Down 解题报告
- 框架 day36 Spring3 入门,DI依赖注入,装配bean基于xml/注解, 整合Junit4,配置约束自动提示
- View的两种更新方法-从源码角度分析invalidate()和postInvalidate()的区别
- 总结协查上海银行绑卡失败原因的处理过程
- LeetCode 155. Min Stack(最小栈)
- Iwfu-安卓重要的生命周期图和流程图
- LeetCode 156. Binary Tree Upside Down(树的倒转)
- Java基础:循环迭代Array/List/Set/Map集合 增强for循环
- 我的四轴专用PID参数整定方法及原理---超长文慎入(转)
- Jenkins进阶系列之——01使用email-ext替换Jenkins的默认邮件通知
- 设计模式 —— 工厂模式(Factory Pattern)
- <<数据结构和离散数学>>树的等价关系,集合论学习
- cell重用机制
- LeetCode 157. Read N Characters Given Read4(读文件流)
- LeetCode 158. Read N Characters Given Read4 II - Call multiple times(读文件流)