Algorithms/- LeetCode

LeetCode - Trim a Binary Search Tree [Java]

자굿 2022. 4. 16. 00:59
 

Trim a Binary Search Tree - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

정답(Solution)

 

class Solution {
    public TreeNode trimBST(TreeNode root, int low, int high) {
        if (root == null) return root;
        if (root.val > high) return trimBST(root.left, low, high);
        if (root.val < low) return trimBST(root.right, low, high);

        root.left = trimBST(root.left, low, high);
        root.right = trimBST(root.right, low, high);
        return root;       
    }
}

 

분석

  • 이진 탐색 트리에서 high와 low 사이 범위의 상대적 구조를 유지하며 범위 밖의 node를 제거
반응형