문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
이진 탐색 트리 root가 주어졌을 때, 트리의 가장 왼쪽 노드가 루트가 되고, 모든 노드는 왼쪽에 자식이 없고 오른쪽 자식이 하나만 있도록 중위 순회 트리로 재배열해라.
#1
Input: root = [5, 3, 6, 2, 4, null, 8, 1, null, null, null, 7, 9]
Output: [1, null, 2, null, 3, null, 4, null, 5, null, 6, null, 7, null, 8, null, 9]
#2
Input: root = [5, 1, 7]
Output: [1, null, 5, null, 7]
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode increasingBST(TreeNode root) {
return increasingBST(root, null);
}
public TreeNode increasingBST(TreeNode root, TreeNode node) {
if(root == null){
return node;
}
TreeNode result = increasingBST(root.left, root);
root.left = null;
root.right = increasingBST(root.right, node);
return result;
}
}