Old discuss is read-only now. Please go to New LeetCode Discuss for your questions and answers!

User account in old discuss will not integrate to the new one, but new discuss is integrated with new online judge, which means, if you already have an account in new online judge, you can access new discuss immediately!

If you want to ask for question relevant to the posts in old discuss, please copy content as part of your question, only old discuss link is NOT ALLOWED!

Please read the FAQ in new LeetCode Discuss to help yourself making the best use of Discuss!

0
1

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

click to show hints.

Hints:

If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.

asked 14 Oct '12, 23:37

1337c0d3r's gravatar image

1337c0d3r ♦♦
1.2k1384171
accept rate: 2%

closed 25 Oct '13, 06:41

The question has been closed for the following reason "bulk close" by 1337c0d3r 25 Oct '13, 06:41


123next »

class Solution {
public:
void flatten(TreeNode *root) {
    if (!root) return;

    TreeNode* left = root->left;
    TreeNode* right = root->right;

    if (left) {
        root->right = left;
        root->left = NULL;

        TreeNode* rightmost = left;
        while(rightmost->right) {rightmost = rightmost->right;}
        rightmost->right = right; // point the right most to the original right child
    }

    flatten(root->right);        
}
};
link

answered 31 Dec '12, 10:34

Daniel%20Qiu's gravatar image

Daniel Qiu
10616
accept rate: 0%

/ * Definition for binary tree * struct TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */

class Solution { public: void flatten(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function flat(root); }

TreeNode *flat(TreeNode *node) {
    if (!node) return NULL;
    TreeNode *leftail = flat(node -> left);
    TreeNode *rightail = flat(node -> right);
    if (leftail) leftail -> right = node -> right;
    if (node -> left) {
        node -> right = node -> left;
        node -> left = NULL;
    }
    if (rightail) return rightail;
    if (leftail) return leftail;
    return node;
}

};

link

answered 31 Dec '12, 04:16

duanmao's gravatar image

duanmao
611
accept rate: 0%

class Solution {

public:

void flatten(TreeNode *root) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    TreeNode* temp = NULL;
    postorder(root, temp);
    }

void postorder(TreeNode* node , TreeNode*& temp) {

    if (node != NULL){
        postorder(node->right,temp);
        postorder(node->left,temp);
        node->right = temp;
        temp = node;
        node->left = NULL;

    }

}

};

link

answered 30 Mar '13, 04:48

ramkumarpvs's gravatar image

ramkumarpvs
211
accept rate: 0%

  /* recurrence: 
   * 1) if current node is null or leaf, return it.
   * 2) get the left child and right child of current node, 
   * 2.1)if the left is not null, set the left as the current node's right, 
   * flat the left tree. return the tail of left child tree as current node.
   * 2.2)if the right is not null, set the right as the current node's right, 
   * flat the right tree. return the tail of left child tree as current node.
   *   
   */
  public TreeNode flatten_recurr(TreeNode root) {
    if (root == null || (root.left == null && root.right == null))
      return root;

    TreeNode left = root.left;
    TreeNode right = root.right;

    root.left = null;

    if (left != null) {
      root.right = left;
      root = flatten_recurr(left);
    }

    if (right != null) {
      root.right = right;
      root = flatten_recurr(right);
    }

    return root;
  }
link

answered 30 Jan '13, 17:03

happyson10's gravatar image

happyson10
503
accept rate: 12%

  /*
   * preOrder:
   * 1) check, if current node is null or leaf, return it.
   * 2) init a stack to store the right child tree
   * 3) preOrder travel the tree
   * 3.1) if current node has left child, store the right child in stack, 
   *      change the left child to right.  
   * 3.2) else, get the right child from the stack and 
   *      set it as the right child as the current node
   *
   */
  public void flatten_preOrder(TreeNode root) {
    if (root == null || (root.left == null && root.right == null))
      return root;

    Stack<TreeNode> stack = new Stack<TreeNode>();
    TreeNode curr = root;

    while (curr != null || !stack.isEmpty()) {

      while (curr.left != null) {
        if(curr.right != null)  
            stack.push(curr.right);

        curr.right = curr.left;
        curr.left = null;
        curr = curr.right;
      }

      if (curr.right == null && !stack.isEmpty()) 
        curr.right = stack.pop();

      curr = curr.right;
    }
  }
link

answered 30 Jan '13, 17:12

happyson10's gravatar image

happyson10
503
accept rate: 12%

A Java Solution:

public class Solution {
    public void flatten(TreeNode root) {
        flattenAndReturn(root);
    }

    private TreeNode flattenAndReturn(TreeNode root)
    {
        if (root == null)
            return null;

        TreeNode leftTail = flattenAndReturn(root.left);
        if(leftTail == null)
        {
            leftTail = root;
        }

        TreeNode rightTail = flattenAndReturn(root.right);
        if(rightTail == null)
        {
            rightTail = leftTail;
        }

        TreeNode temp = root.right;
        root.right = root.left;
        leftTail.right = temp;
        root.left = null;
        return rightTail;
    }
}
link

answered 05 Feb '13, 12:12

Newton's gravatar image

Newton
112
accept rate: 0%

public class Solution {
public TreeNode flattenT(TreeNode root) {
    if (root==null) return null;
    if (root.left==null && root.right==null) return root;
    TreeNode pleft, pright,pred;
    if (root.left!=null) {
        pleft = root.left;
        pred = flattenT(pleft);
        root.left=null;
        if (root.right!=null) {
            pright=root.right;
            root.right = pleft;
            pred.right = pright;
            pred = flattenT(pright);
            return pred;
        } else {
            root.right = pleft;
            return pred;
        }
    } else {
        pred = flattenT(root.right);
        return pred;
    }

}
public void flatten(TreeNode root) {
    // Start typing your Java solution below
    // DO NOT write main() function
    TreeNode t = flattenT(root);
    return;
}
}
link

answered 06 Feb '13, 19:12

jjlights's gravatar image

jjlights
102
accept rate: 0%

void flatten(TreeNode *root) {
    if(!root) return;
    get(root,root);
}
void get(TreeNode *root, TreeNode *& p){
    if(!root) return;

    TreeNode *l=root->left;
    TreeNode *r=root->right;

    if(root!=p){
        p->left=NULL;
        p->right=root;
        p=root;}
    get(l,p);
    get(r,p);   
}
link

answered 03 Mar '13, 14:56

zyfo2's gravatar image

zyfo2
113
accept rate: 0%

edited 03 Mar '13, 14:58

/**
 * link the node in reverse pre-order traversal
 * right first followed by left then middle.
 * link the node visited with its previous node.
 */
public static void linkNode(TreeNode previous, TreeNode node){

    if(node.right !=null){
        if(node.left != null){
            TreeNode rightMost = findRightMost(node.left);
            linkNode(rightMost, node.right);
        }else{
            linkNode(node, node.right);
        }
    }

    if(node.left != null){
        linkNode(node, node.left); 
    }

    if(previous != null){
        previous.right = node;
        previous.left = null;
    }

    return;

}

//find the rightmost child of the subtree under the node.
public static TreeNode findRightMost(TreeNode node){
    while(node != null){
        if(node.left == null && node.right == null){
            return node;
        }else if(node.right != null){
            node = node.right;
        }else if(node.left != null){
            node = node.left;
        }
    }
    return null;
}
link

answered 12 Mar '13, 00:14

mapmatching's gravatar image

mapmatching
1
accept rate: 0%

i have problem!!!

TreeNode *last = NULL;
void solute_node(TreeNode *current){
    TreeNode *left = current ->left;
    TreeNode *right = current ->right;
    current ->left = NULL;
    current ->right = NULL;
    if (last != NULL){
        last ->right = current;
    }
    last = current;
    if (left != NULL){
        solute_node(left);
    }
    if (right != NULL){
        solute_node(right);
    }
}

void flatten(TreeNode *root) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    if (root == NULL){
        return;
    }
    solute_node(root);
}

this code sucess with vc++9.0 and g++3.4.4, but has runtime error. could anyone help me to fix this problem?

link

answered 30 Mar '13, 09:55

goshan's gravatar image

goshan
11
accept rate: 0%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • Indent code by 4 spaces.
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×133

Asked: 14 Oct '12, 23:37

Seen: 6,069 times

Last updated: 25 Oct '13, 06:41