Flatten Binary Tree to Linked List

时间:2015-03-29 12:05:49   收藏:0   阅读:104

Flatten Binary Tree to Linked List

问题:

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

思路:

  前序pre-order遍历+prenode

我的代码:

技术分享
public class Solution {
    public void flatten(TreeNode root) {
        if(root == null)    return;
        if(pre != null)
        {
            pre.left = null;
            pre.right = root;
        }
        pre = root;
        TreeNode left = root.left;
        TreeNode right = root.right;
        flatten(left);
        flatten(right);
    }
    TreeNode pre = null;
}
View Code

学习之处:

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!