import java.util.*; /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public TreeNode Convert(TreeNode root) { if (root == null) { return null; } List<TreeNode> list = inOrder(root); TreeNode head = list.get(0); head.left = null; TreeNode pre = head; for (int i = 1; i < list.size(); i++) { TreeNode cur = list.get(i); pre.right = cur; cur.left = pre; pre = cur; } list.get(list.size() - 1).right = null; return head; } private List<TreeNode> inOrder(TreeNode root) { List<TreeNode> res = new ArrayList<>(); if (root == null) { return res; } Stack<TreeNode> stack = new Stack<>(); TreeNode cur = root; while (!stack.isEmpty() || cur != null) { if (cur != null) { stack.push(cur); cur = cur.left; } else { cur = stack.pop(); res.add(cur); cur = cur.right; } } return res; } }