Flatten Binary Tree to Linked List
来源:互联网 发布:手机编程用什么软件 编辑:程序博客网 时间:2024/06/08 05:34
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
非递归:
public class Solution { public void flatten(TreeNode root) { TreeNode now = root; while(now != null){ if(now.left != null){ TreeNode pre = now.left; while(pre.right != null){ pre = pre.right; } pre.right = now.right; now.right = now.left; now.left = null; } now = now.right; } }}
递归:
public class Solution { TreeNode pre = null; public void flatten(TreeNode root) { if(root == null) return; flatten(root.right); flatten(root.left); root.right = pre; root.left = null; pre = root; }}
0 0
- LeetCode: Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- LeetCode: Flatten Binary Tree to Linked List
- [Leetcode] Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- [LeetCode] Flatten Binary Tree to Linked List
- Leetcode: Flatten Binary Tree to Linked List
- leetcode Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- LeetCode - Flatten Binary Tree to Linked List
- 【leetcode】Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- [LeetCode]Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- 九度oj 1057
- 创建型模式:抽象工厂模式(Abstract Factory Pattern)
- 2. JavaScript Boolean 对象
- java json
- 基本排序算法java实现
- Flatten Binary Tree to Linked List
- swift-学习之路,字典、数组、循环-控制流
- excel日期编辑问题
- MQ开启密码访问平台服务步骤
- 笔记-平时总结
- (转载)NumPy详细教程
- 同余式和中国剩余定理
- 10048 - Audiophobia (Floyd)
- javascript delete 数组长度不变