JS 숙련도를 위하여 JS로 별도로 풀어봤다.
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, p, q) {
if (p.val < root.val && q.val < root.val) {
return lowestCommonAncestor(root.left,p,q)
}
else if( p.val > root.val && q.val > root.val) {
return lowestCommonAncestor(root.right,p,q)
}
else {
return root
}
};'TIL > 이전 풀이' 카테고리의 다른 글
| [js] Programmers_Lv.0_인덱스 바꾸기 (0) | 2023.03.28 |
|---|---|
| [js] Programmers_Lv.0_자릿수 더하기 (1) | 2023.03.28 |
| [python] leetcode_235. Lowest Common Ancestor of a Binary Search Tree (2) | 2023.03.06 |
| [python] Leetcode 46. Permutations (0) | 2022.12.13 |
| [python] Leetcode 49. Group Anagrams (2) | 2022.12.13 |