Flatten Binary Tree to Linked List
Flatten a binary tree to a fake "linked list" in pre-order traversal.
Here we use the right pointer in TreeNode as the next pointer in ListNode.
Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
分析:
把所有的树节点按照PRE-ORDER的顺序存在ArrayList里面,然后遍历ArrayList里的节点,调整它们的left and right child指针。
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
* cnblogs.com/beiyeqingteng
*/
public void flatten(TreeNode root) {
// write your code here
ArrayList<TreeNode> list = new ArrayList<TreeNode>();
preOrder(root, list); for (int i = ; i < list.size(); i++) {
list.get(i).left = null;
if (i == list.size() - ) {
list.get(i).right = null;
} else {
list.get(i).right = list.get(i + );
}
}
} public void preOrder(TreeNode root, ArrayList<TreeNode> list) {
if (root != null) {
list.add(root);
preOrder(root.left, list);
preOrder(root.right, list);
}
}
}
自己后来写出的方法:
public class Solution { public void flatten(TreeNode root) {
helper(root);
} public TreeNode helper(TreeNode root) {
if (root == null) return root;
TreeNode leftHead = helper(root.left);
TreeNode rightHead = helper(root.right);
root.left = null;
if (leftHead != null) {
root.right = leftHead;
TreeNode current = root;
while (current.right != null) {
current = current.right;
}
current.right = rightHead;
}
return root;
}
}
还有一种是递归的方式,下面的方法参考了另一个网友的做法,觉得这种方法非常容易理解,而且具有一般性。
解法2:递归构建
假设某节点的左右子树T(root->left)和T(root->right)已经flatten成linked list了:
1
/ \
2 5
\ \
3 6 <- rightTail
\
4 <- leftTail
如何将root、T(root->left)、T(root->right) flatten成一整个linked list?显而易见:
temp = root->right
root->right = root->left
root->left = NULL
leftTail->right = temp
这里需要用到已经flatten的linked list的尾部元素leftTail, rightTail。所以递归返回值应该是生成的整个linked list的尾部。
class Solution {
public void flatten(TreeNode root) {
flattenBT(root);
} TreeNode flattenBT(TreeNode root) {
if(root == null) return null;
TreeNode leftTail = flattenBT(root.left);
TreeNode rightTail = flattenBT(root.right);
10 if(root.left != null) {
TreeNode temp = root.right;
root.right = root.left;
root.left = null;
leftTail.right = temp;
} 17 if(rightTail != null) return rightTail;
18 if(leftTail != null) return leftTail;
19 return root;
}
}
需注意几个细节
ln 10:只有当左子树存在时才将它插入右子树中
ln 17-19:返回尾部元素时,需要特殊处理 (1) 有右子树的情况,(2) 无右子树但有左子树的情况,(3)左右子树均不存在的情况。
Reference:
http://bangbingsyb.blogspot.com/2014/11/leetcode-flatten-binary-tree-to-linked.html
Flatten Binary Tree to Linked List的更多相关文章
- [LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...
- 31. Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
- Flatten Binary Tree to Linked List (LeetCode #114 Medium)(LintCode #453 Easy)
114. Flatten Binary Tree to Linked List (Medium) 453. Flatten Binary Tree to Linked List (Easy) 解法1: ...
- 【LeetCode】Flatten Binary Tree to Linked List
随笔一记,留做重温! Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-pl ...
- 114. Flatten Binary Tree to Linked List(M)
. Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ...
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- Leetcode:Flatten Binary Tree to Linked List 解题报告
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
- [LeetCode]Flatten Binary Tree to Linked List题解(二叉树)
Flatten Binary Tree to Linked List: Given a binary tree, flatten it to a linked list in-place. For e ...
- 【LeetCode】114. Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
- leetcode dfs Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...
随机推荐
- if...else语句的应用
用户输入身高判断身体状况的题目 题目中用到的公式:体重=身高-100±3»»推出:3>体重-身高+100>-3 namespace ConsoleApplication2 { class ...
- 【BZOJ-4407】于神之怒加强版 莫比乌斯反演 + 线性筛
4407: 于神之怒加强版 Time Limit: 80 Sec Memory Limit: 512 MBSubmit: 241 Solved: 119[Submit][Status][Discu ...
- [IOS 同步GET和POST数据的模版]
1.同步GET (联网检测) +(BOOL)checkConnect{ // 初始化请求 NSMutableURLRequest *request = [[NSMutableURLRequest al ...
- Android 设计模式 之 观察者模式
/* * 观察者模式 * 定义对象间的一种一个(Subject)对多(Observer)的依赖关系,当一个对象的状态发送改变时,所以依赖于它的 * 对象都得到通知并被自动更新 * * 当然, ...
- Laravel5.1 启动详解
借鉴: Laravel所有请求的入口文件是:/public/index.php,代码如下 <?php /*|------------------------------------------- ...
- 从js的repeat方法谈js字符串与数组的扩展方法
js将字符串重复N次的repeat方法的8个版本 /* *@desc: 将一个字符串重复自身N次 */ //版本1:利用空数组的join方法 function repeat(target, n) { ...
- POJ1703Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37722 Accepted: ...
- ExtJS入门教程06,grid分页的实现
前面两篇内容分别介绍了extjs grid的基本用法和extjs grid异步加载数据,这篇文章将介绍extjs grid的分页. 数据量大的时候我们必须用到分页,结合上一篇的异步加载数据,今天我们就 ...
- [工作积累] Google/Amazon平台的各种坑
所谓坑, 就是文档中没有标明的特别需要处理的细节, 工作中会被无故的卡住各种令人恼火的问题. 包括系统级的bug和没有文档化的限制. 继Android的各种坑后, 现在做Amazon平台, 遇到的坑很 ...
- zabbix 分布式监控(proxy)源码安装
安装分布式监控(代理节点) 1.下载软件zabbix-3.2.1.tar.gz 1.1 解压 wget http://nchc.dl.sourceforge.net/project/zabbix/ZA ...