Flatten Binary Tree to Linked List

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

学习之处:

  • 转换成常见的前序遍历问题,然后再加上一个prenode指针就齐活了。

你可能感兴趣的:(binary)