Easy 1ms Java DFS solution


  • 17
    Y
     public class Solution {
        public void flatten(TreeNode root) {
            if(root==null)
                return;
            flatten(root.left);
            flatten(root.right);
            TreeNode left  = root.left;
            TreeNode right = root.right;
            root.left  = null;
            root.right = left; 
            while(root.right!=null)
                root = root.right;
            root.right = right;
        }
    
    }

  • 0
    P

    Is this a method of divide and conquer?


Log in to reply
 

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