AC java solution


  • 0
    Y
    public class Solution {
    public void flatten(TreeNode root) {
        if(root == null) return;
        List<TreeNode> list = new ArrayList<TreeNode>();
        flatHelper(root,list);
        for(int i = 0; i < list.size(); i++)
        {
            root.val = list.get(i).val;
            if(i < list.size() - 1)
            {
                TreeNode temp = new TreeNode(0);
                root.right = temp;
                root.left = null;
                root = root.right;
            }
        }
    }
    
    private void flatHelper(TreeNode root, List<TreeNode> list){
        if(root == null) return;
        list.add(root);
        flatHelper(root.left,list);
        flatHelper(root.right,list);
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.