File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-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+ // ํธ๋ฆฌ ์ํ -> ์ฌ๊ท, ํ, ์คํ ๋ฐฉ์๋ค์ด ์์.
16+ // ๋๊ฐ์ ํธ๋ฆฌ๋ฅผ ๋์์ ์กฐํํ๋ ํ์ด
17+
18+ // ๋ ๋ค null์ด๋ฉด ๊ฐ์
19+ if ( p === null && q === null ) return true ;
20+
21+ // ํ๋๋ง null์ด๋ฉด ๋ค๋ฆ
22+ if ( p === null || q === null ) return false ;
23+
24+ // ๊ฐ์ด ๋ค๋ฅด๋ฉด ๋ค๋ฆ
25+ if ( p . val !== q . val ) return false ;
26+
27+ // ์ผ์ชฝ ์์๋ค๊ณผ ์ค๋ฅธ์ชฝ ์์๋ค์ด ๋ชจ๋ ๊ฐ์์ผ ํจ
28+ return isSameTree ( p . left , q . left ) && isSameTree ( p . right , q . right ) ;
29+ } ;
30+
31+ // ์๊ฐ๋ณต์ก๋ O(N)
32+ // ๊ณต๊ฐ๋ณต์ก๋ O(N)
You canโt perform that action at this time.
0 commit comments