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 ...
随机推荐
- DOM0,DOM2,DOM3事件,事件基础知识入门
事件是javascript和HTML交互基础, 任何文档或者浏览器窗口发生的交互, 都要通过绑定事件进行交互; 事件有DOM0, DOM2和DOM3的区分(别问我怎么少了一个DOM1, 也没找到DOM ...
- java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
在运行定时任务的时候报错: Java.lang.NoClassDefFoundError: javax/transaction/UserTransaction 原因:缺少jta的jar包 解决方法:下 ...
- Java算法-希尔排序
希尔排序的诞生是由于插入排序在处理大规模数组的时候会遇到需要移动太多元素的问题.希尔排序的思想是将一个大的数组“分而治之”,划分为若干个小的数组,以 gap 来划分,比如数组 [1, 2, 3, 4, ...
- Dinic问题
问题:As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Offic ...
- HDU #5507 GT and Strings
这是AC自动机系列的第一篇 传送门 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Othe ...
- C++处理一个动态规划的问题
嗯哼,别人问的问题,看的我也头晕,百度了一下动态规划,看了看才想起来该怎么做,今天写了写代码,实现了~ 要求是递归,动态规划,想了想这种方法也是最简单的~ 所谓动态规划:把多阶段过程转化为一系列单阶段 ...
- 检测ADO.net拼接字符串中非法字符
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Refle ...
- spark.SecurityManager: SecurityManager: authentication disabled
- js中网页图片自动更换的效果
<script> var arr=new Array(); arr[]="url(images/city.jpg)"; arr[]="url(images/d ...
- Swift定义单例
而在Swift中我们通过清晰的语法便能定义类变量: 通过static定义的类变量无法在子类重写,通过class定义的类变量则可在子类重写. struct SomeStructure { static ...