clueless coding // TODO: be smarter

LeetCode Flatten Binary Tree to Linked List: Java Solution



TL;DR Code Solution


public void flatten(TreeNode root) {

    TreeNode runner = root;

    while (runner != null) {

        if (runner.left != null) {
            TreeNode temp = runner.right;
            runner.right = runner.left;
            runner.left = null;
            TreeNode righter = runner;
            while (righter != null && righter.right != null) {
                righter = righter.right;
            }
            righter.right = temp;
        }

        runner = runner.right;

    }



}