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
Subscribe to see which companies asked this question
题解:
code:
public class Solution {
public void flatten(TreeNode root) {
if( root != null)
{
flatten(root.right);
if(root.left != null)
{
flatten(root.left);
TreeNode node = root.left;
while(node.right != null)
{
node = node.right;
}
node.right = root.right;
root.right = root.left;
root.left = null;
}
}
}
}