1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
class Solution { public TreeNode increasingBST(TreeNode root) { List<TreeNode> list = new ArrayList<>(); inOrder(root, list); TreeNode ans = new TreeNode(-1), p = ans; for (TreeNode node : list) { p.right = node; p = node; } return ans.right; }
private void inOrder(TreeNode root, List<TreeNode> list) { if (root == null) return; inOrder(root.left, list); list.add(new TreeNode(root.val)); inOrder(root.right, list); } }
|