DEV Community

loading...

Discussion on: Solution: Flatten Binary Tree to Linked List

Collapse
rohithv07 profile image
Rohith V

My Java Solution using Recursion

class Solution {
    private TreeNode previous = null;
    public void flatten(TreeNode root) {
        if (root == null)
            return;
        flatten(root.right);
        flatten(root.left);
        root.right = previous;
        root.left = null;
        previous = root;
    }

}
Enter fullscreen mode Exit fullscreen mode