The idea is very simple:

flatten left subtree

flatten right subtree

concatenate root > left flatten subtree > right flatten subtree
public void flatten(TreeNode root) { if(root == null) return; flatten(root.left); flatten(root.right); // save current right for concatination TreeNode right = root.right; if(root.left != null) { // step 1: concatinate root with left flatten subtree root.right = root.left; root.left = null; // set left to null // step 2: move to the end of new added flatten subtree while(root.right != null) root = root.right; // step 3: contatinate left flatten subtree with flatten right subtree root.right = right; } }