114. Flatten Binary Tree to Linked List

https://leetcode.com/problems/flatten-binary-tree-to-linked-list/#/description

这题的解法真是八仙过海各显神通。。这题递归的思维难度有点大。。但是覃超说只需要理解成把左边神棍打平接到root的右边,然后把右边的神棍打平接到左边神棍的底部就行了。但是他这个描述跟pre-order这个特性有关系吗。。

引用小莹子的递归代码。

TreeNode lastvisited = null;
    public void flatten(TreeNode root) {
        if(root == null)
            return;
        
        TreeNode realright = root.right;
        if(lastvisited != null){
            lastvisited.left = null;
            lastvisited.right = root;
        }
        
        lastvisited = root;
        flatten(root.left);
        flatten(realright);
    }

http://blog.csdn.net/sbitswc/article/details/26540269

你可能感兴趣的:(114. Flatten Binary Tree to Linked List)