File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * function TreeNode(val, left, right) {
4+ * this.val = (val===undefined ? 0 : val)
5+ * this.left = (left===undefined ? null : left)
6+ * this.right = (right===undefined ? null : right)
7+ * }
8+ */
9+ /**
10+ * @param {TreeNode } p
11+ * @param {TreeNode } q
12+ * @return {boolean }
13+ */
14+ var isSameTree = function ( p , q ) {
15+ const dfs = ( a , b ) => {
16+ const isValSame = a ?. val === b ?. val ;
17+ const isLeftValSame = a ?. left ?. val === b ?. left ?. val ;
18+ const isRightValSame = a ?. right ?. val === b ?. right ?. val ;
19+
20+ if ( ! isValSame || ! isLeftValSame || ! isRightValSame ) {
21+ return true ;
22+ }
23+
24+ if ( a ?. left && b ?. left ) {
25+ const isLeftDiff = dfs ( a . left , b . left ) ;
26+
27+ if ( isLeftDiff ) {
28+ return true ;
29+ }
30+ }
31+
32+
33+ if ( a ?. right && b ?. right ) {
34+ const isRightDiff = dfs ( a . right , b . right ) ;
35+
36+ if ( isRightDiff ) {
37+ return true ;
38+ }
39+ }
40+
41+ }
42+
43+
44+ return ! dfs ( p , q ) ;
45+ } ;
46+
47+ // ์๊ฐ๋ณต์ก๋ - O(n) p์ q๊ฐ ๊ฐ๋ค๋ฉด ๋ชจ๋ ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธํ๋ฏ๋ก
48+ // ๊ณต๊ฐ๋ณต์ก๋ - O(h) ๊น์ด์ฐ์ ํ์์ ์ฌ์ฉํ์ฌ ํธ๋ฆฌ์ ์ต๋ ๋์ด๋งํผ ์คํํ๊ฒฝ์ด ํจ์ํธ์ถ์คํ์ ์ ์ฅ๋๋ฏ๋ก
You canโt perform that action at this time.
0 commit comments