LeetCode – Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.

For example,

        / \
       2   5
      / \   \
     3   4   6

The flattened tree should look like:



Go down through the left, when right is not null, push right to stack.

Java Solution

 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
public class Solution {
    public void flatten(TreeNode root) {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode p = root;
        while(p != null || !stack.empty()){
            if(p.right != null){
            if(p.left != null){
                p.right = p.left;
                p.left = null;
            }else if(!stack.empty()){
                TreeNode temp = stack.pop();
            p = p.right;
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Bharath Chandra

    void flatten(struct TreeNode* root) {
    struct TreeNode *temp = NULL;
    if(root-> left){
    flatten(root-> left);
    if(root-> right){
    flatten(root-> right);
    temp = root-> left;
    while(temp-> right){
    temp = temp-> right;
    temp-> right = root-> right;
    root-> right = root-> left;
    root-> left = NULL;
    flatten(root-> right);


  • Matias SM

    Recursive solution. Time complexity O(N), Space complexity O(H) (H: height of the tree)

    public class BinTreeNode {
    public final T value;
    public BinTreeNode left;
    public BinTreeNode right;

    void flatten(BinTreeNode root) {
    flatten(root, null);

    BinTreeNode flatten(BinTreeNode node, BinTreeNode last) {
    if (node == null) return last;
    BinTreeNode oldLeft = node.left;
    BinTreeNode oldRight = node.right;
    node.left = last;
    if (last != null) {
    last.right = node;
    last = node;
    last = flatten(oldLeft, last);
    last = flatten(oldRight, last);
    return last;

  • Ashish Pai

    Using recursion is not in-place right? You are using the system stack.

  • Almas Daumov

    @diegocedrim:disqus your code is good, but missing 1 thing – setting “node.left = nil;” before calling recursion. See http://pastebin.com/ubpZaXqf

  • RoroZoro

    Had the same doubt. But since the problem is to “flatten” which means, we need to change the links pointed by the node, we need to do like this. But yeah, preorder gives the same answer.

  • Noobaka

    I mean PreOrder Traversal.

  • Noobaka

    I am a tad-bit confused. Isn’t this question very similar to Inorder traversal of a Binary Tree?Will it work if I just did Inorder traversal and stored all that value while doing that? Am I wrong? Please correct me.

  • hdante

    There’s a non recursive solution that does not use a container, only an auxiliary variable. The idea is to move the right subtree to the left subtree to free the root’s right node, then move the resulting left subtree to the right side.

  • very incomplete code, it doesn’t run without those accessory classes.

  • rampartmovie

    Just do an inorder traversal and then add the left node to the list. and when the node pops out and u assign right node, assign next to list again.

  • rampartmovie

    Just do an inorder traversal and then add the left node to the list. and when the node pops out and u assign right node, assign next to list again.

  • Diego Cedrim

    I wrote a code that not uses any other data structure, but uses recursion.

    The basic idea is:

    1) Find a node N with a left child L;
    2) Find the rightmost descendant of L and call it RM;
    3) Aux = N.right;
    4) N.right = L
    5) RM.right = Aux
    6) Explore the right subtree of N recursively.

    The code can be seen in http://pastebin.com/AXsgEJs7

  • Kartrace

    is this in place? you are using a stack…, but the solution is indeed nice!