2014年2月13日星期四

LeteCode - Flatten Binary Tree to Linked List

 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

/**
 * 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) {
        doFlatten(root);
    }
    private TreeNode doFlatten(TreeNode root) {
        if(root==null) {
            return null;
        }
        TreeNode tail = root;
        while(true) {
            if(tail.right!=null) {
                tail = tail.right;
            } else if(tail.left!=null) {
                tail = tail.left;
            } else {
                break;
            }
        }
        if(root.left==null && root.right!=null) {
            doFlatten(root.right);
        }
        if(root.left!=null) {
            TreeNode lTail = doFlatten(root.left);
            TreeNode right = root.right;
            doFlatten(root.right);
            root.right = root.left;
            root.left = null;
            lTail.right = right;
        } 
        return tail;
    }
}


public void flatten(TreeNode root) {
        if (root == null) return;

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

        root.left = null;

        flatten(left);
        flatten(right);

        root.right = left;
        TreeNode cur = root;
        while (cur.right != null) cur = cur.right;
        cur.right = right;
    }

=====

public class Solution {
    public void flatten(TreeNode root) {
        Stack<TreeNode> stk = new Stack<TreeNode>();
        if (root == null) {
            return;
        }
        TreeNode pre = null;
        stk.push(root);
        while (!stk.isEmpty()) {
            TreeNode node = stk.pop();
            if (pre != null) {
                pre.right = node;
                pre.left = null;
            }
            pre = node;
            if (node.right != null) {
                stk.push(node.right);
            }
            if (node.left != null) {
                stk.push(node.left);
            }
        }
    }
}
=====

public class Solution {
    public void flatten(TreeNode root) {
        while (root != null) {
            TreeNode oldLeft = root.left;
            TreeNode oldRight = root.right;
            
            root.left = null;
            root.right = oldLeft;
            
            TreeNode tail = oldLeft;
            while (tail != null && tail.right != null) {
                tail = tail.right;
            }
            if (tail != null) {
                tail.right = oldRight;
            } else {
                root.right = oldRight;
            }
            root = root.right;
        }
    }
}

没有评论:

发表评论