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 ...
随机推荐
- htpasswd 详解
Apache htpasswd命令用法详解 htpasswd建立和更新存储用户名.密码的文本文件, 用于对HTTP用户的basic认证. # /usr/local/apache/bin/htpassw ...
- 在windows 下使用git
首先安装好在windows下的linux模拟交互器 这里我选择的是cygwin 这里我是参考:http://book.51cto.com/art/201107/278731.htm 这里还要注意我这里 ...
- OC基础--description方法
PS:经过之类重写description方法后,个人感觉有点像C#中的ToString();方法 一.description方法的作用:(输出所有的OC对象都用%@) 1.默认情况下(不重写descr ...
- 本地的手机号码归属地查询-oracle数据
最近做的项目中,有个功能是手机归属地查询,因为项目要在内网下运行,所以不能用提供的webservice,只好在网上找手机归属地的数据,很多都是access的,我们的项目是用oracle,只好自己转吧, ...
- slf4j和log4j配置
SLF4J即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统.按照官方的说法,SLF4J是一个用于日志系统的简单Fac ...
- Linux强制访问控制机制模块分析之mls_type.h
2.4.2 mls_type.h 2.4.2.1文件描述 对于mls_type.h文件,其完整文件名为security/selinux/ss/mls_types.h,该文件定义了MLS策略使用的类型. ...
- 深入浅出Redis04使用Redis数据库(lists类型)
一 lists类型及操作 List是一个链表结构,主要功能是push,pop,获取一个范围的所有值等等,操作中key理解为链表的名字. Redis的list类型其实就是一个每个子元素都是sring类 ...
- poj1631Bridging signals(最长单调递增子序列 nlgn)
Bridging signals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12251 Accepted: 6687 ...
- PHP利用Filesystem函数实现操作缓存(生成,获取,删除操作)
<?php class File{ //$key 相当于缓存文件的文件名 //$value 缓存数据 private $_dir;//定义默认路径 const EXT='.txt'; publi ...
- ssh项目删除
1.在页面添加删除跳转 <td><a href="deleteStandard.action?id=${sta.id }">删除</a>< ...