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
| /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode increasingBST(TreeNode root) { List <Integer> list = new ArrayList<>(); inorder(root, list); TreeNode ans = new TreeNode(0), cur = ans; for (int v : list) { cur.right = new TreeNode(v); cur = cur.right; } return ans.right; }
public void inorder(TreeNode root, List <Integer> list) { if (root == null) { return; } inorder(root.left, list); list.add(root.val); inorder(root.right, list); } }
|