File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
lowest-common-ancestor-of-a-binary-search-tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ์๊ฐ ๋ณต์ก๋: ํธ๋ฆฌ์ ํ์ชฝ ๊ฒฝ๋ก๋ฅผ ์ ํํ๋ฉด์ ํ์ํ๋ฏ๋ก, ํธ๋ฆฌ์ ๋์ด๊ฐ h๋ฉด O(h)
3+ * ๊ณต๊ฐ ๋ณต์ก๋: ์ฌ๊ท ํธ์ถ ์คํ์ ๊น์ด๋ ๊ท ํ ์กํ ํธ๋ฆฌ์ ๊ฒฝ์ฐ O(logn), ํธํฅ๋ ํธ๋ฆฌ๋ O(n) ==> O(h)
4+ */
5+ /**
6+ * Definition for a binary tree node.
7+ * function TreeNode(val) {
8+ * this.val = val;
9+ * this.left = this.right = null;
10+ * }
11+ */
12+ /**
13+ * @param {TreeNode } root
14+ * @param {TreeNode } p
15+ * @param {TreeNode } q
16+ * @return {TreeNode }
17+ */
18+ var lowestCommonAncestor = function ( root , p , q ) {
19+ const dfs = ( node , p , q ) => {
20+ if ( node . val > p . val && node . val > q . val ) {
21+ return dfs ( node . left , p , q ) ;
22+ }
23+ if ( node . val < p . val && node . val < q . val ) {
24+ return dfs ( node . right , p , q ) ;
25+ }
26+ return node ;
27+ }
28+ return dfs ( root , p , q ) ;
29+ } ;
30+
You canโt perform that action at this time.
0 commit comments