Flatten Binary Tree to Linked List
来源:互联网 发布:数组结束标志 编辑:程序博客网 时间:2024/05/21 19:30
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
实际是变成先序遍历的序列放到右子树
要不断保留本次的root节点,所以采取递归的方式,不然的话下次遍历之后就不能
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
- Android性能优化典范 - 第6季
- 微信小程序--text组件学习
- 提升代码的效率--复用
- 数字证书及其认证过程
- DIV+CSS布局
- Flatten Binary Tree to Linked List
- spring 自定义注解
- 4-姓名拼写
- 土豆烧鸡腿
- 120. Triangle
- 什么是内存抖动
- 指针
- 【GDOI模拟】富爷说是一棵树
- SQL语句for Android iOS Sqlite(视频+笔记+图解)