Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ 2 5
/ \ 3 4 6
The flattened tree should look like: 1
2
3
4
5
6
public class Solution {
public void flatten(TreeNode root) {
if(root==null) return;
flatten(root.left);
flatten(root.right);
TreeNode temp=root.right;
if(root.left!=null){
root.right=root.left;
root.left=null;
while(root.right != null){
root=root.right;
}
root.right=temp;
}
}
}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List
原文地址:http://blog.csdn.net/buptgshengod/article/details/44306461