Binary Tree To Double LinkedList
2013-11-14 00:02
411 查看
Change a binary tree to a double linkedlist.
class Node{
Node left;
Node right;
int val;
}
//Time complexity: O(n^2).
public Node convert(Node root) {
if(root == null) return null;
Node left = convert(root.left);
Node right = convert(root.right);
if(left != null) {
merge(getEnd(left), root);
}
if(right != null) {
merge(root, getEnd(right));
}
return (left == null) ? root : left;
}
public Node getEnd(Node root) {
Node move = root;
if(move == null) return null;
while(move.right != null) {
move = move.right;
}
return move;
}
public void merge(Node left, Node right) {
left.right = right;
right.left = left;
}
//use the circular double linkedlist
//it will use only O(n) time complexity.
public Node convert(Node root) {
if(root == null) return;
Node result = help(root);
//cancle the link of head and tail
result.left.right = null;
result.left = null;
return result;
}
public Node help(Node root) {
if(root == null) return null;
Node left = help(root.left);
Node right = help(root.right);
if(left == null && right == null) {
root.left = root;
root.right = root;
return root;
}
//keep the end of the right
Node rightTail = (right == null) ? null : right.left;
//join left with root
if(left != null) {
merge(left.left, root); //connect the end of left with root
}else {
merge(right.left, root); //connect the end of right with root
}
//join right with right
if(right != null) {
merge(root, right); // connect root with head of right
}else {
merge(root, left); //connect root with head of left
}
//join left with right
if(left != null && right != null) {
merge(rightTail, left);
}
return (left == null) ? root : left;
}
class Node{
Node left;
Node right;
int val;
}
//Time complexity: O(n^2).
public Node convert(Node root) {
if(root == null) return null;
Node left = convert(root.left);
Node right = convert(root.right);
if(left != null) {
merge(getEnd(left), root);
}
if(right != null) {
merge(root, getEnd(right));
}
return (left == null) ? root : left;
}
public Node getEnd(Node root) {
Node move = root;
if(move == null) return null;
while(move.right != null) {
move = move.right;
}
return move;
}
public void merge(Node left, Node right) {
left.right = right;
right.left = left;
}
//use the circular double linkedlist
//it will use only O(n) time complexity.
public Node convert(Node root) {
if(root == null) return;
Node result = help(root);
//cancle the link of head and tail
result.left.right = null;
result.left = null;
return result;
}
public Node help(Node root) {
if(root == null) return null;
Node left = help(root.left);
Node right = help(root.right);
if(left == null && right == null) {
root.left = root;
root.right = root;
return root;
}
//keep the end of the right
Node rightTail = (right == null) ? null : right.left;
//join left with root
if(left != null) {
merge(left.left, root); //connect the end of left with root
}else {
merge(right.left, root); //connect the end of right with root
}
//join right with right
if(right != null) {
merge(root, right); // connect root with head of right
}else {
merge(root, left); //connect root with head of left
}
//join left with right
if(left != null && right != null) {
merge(rightTail, left);
}
return (left == null) ? root : left;
}
相关文章推荐
- Convert Binary Search Tree to an Ordered Double Linked List
- Binary search tree convert to double 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
- 114. Flatten Binary Tree to Linked List
- 114. Flatten Binary Tree to Linked List
- [LeetCode] Convert Binary Search Tree to Sorted Doubly 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
- 114. Flatten Binary Tree to Linked List LeetCode
- 114- Flatten Binary Tree to Linked List
- Binary Tree Preorder, Inorder, Postorder Traversal,Flatten Binary Tree to Linked List
- Leetcode_ Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- Middle-题目53:114. Flatten Binary Tree to Linked List