4
4
* @param {TreeNode } subRoot
5
5
* @return {boolean }
6
6
*/
7
- var isSubtree = function ( root , subRoot ) {
8
- if ( ! root ) return false
7
+ var isSubtree = function ( root , subRoot ) {
8
+ if ( ! root ) return false ;
9
9
10
- if ( isSame ( root , subRoot ) ) return true
10
+ if ( isSame ( root , subRoot ) ) return true ;
11
11
12
- const hasLeftTree = isSubtree ( root . left , subRoot )
13
- const hasRightTree = isSubtree ( root . right , subRoot )
12
+ const hasLeftTree = isSubtree ( root . left , subRoot ) ;
13
+ const hasRightTree = isSubtree ( root . right , subRoot ) ;
14
14
15
- return hasLeftTree || hasRightTree
15
+ return hasLeftTree || hasRightTree ;
16
16
} ;
17
17
18
18
const isSame = ( root , subRoot ) => {
19
- const hasReachedEnd = ! ( root && subRoot )
20
- if ( hasReachedEnd ) return root === subRoot
19
+ const hasReachedEnd = ! ( root && subRoot ) ;
20
+ if ( hasReachedEnd ) return root === subRoot ;
21
21
22
- const isMismatch = root . val !== subRoot . val
23
- if ( isMismatch ) return false
22
+ const isMismatch = root . val !== subRoot . val ;
23
+ if ( isMismatch ) return false ;
24
24
25
- const isLeftSame = isSame ( root . left , subRoot . left )
26
- const isRightSame = isSame ( root . right , subRoot . right )
25
+ const isLeftSame = isSame ( root . left , subRoot . left ) ;
26
+ const isRightSame = isSame ( root . right , subRoot . right ) ;
27
27
28
- return isLeftSame && isRightSame
29
- }
28
+ return isLeftSame && isRightSame ;
29
+ } ;
30
30
31
- const hash = ( val ) => require ( 'crypto' )
32
- . createHash ( 'md5' )
33
- . update ( val )
34
- . digest ( 'hex' )
31
+ const hash = ( val ) =>
32
+ require ( 'crypto' ) . createHash ( 'md5' ) . update ( val ) . digest ( 'hex' ) ;
35
33
36
34
const merkle = ( root ) => {
37
- if ( ! root ) return '#'
35
+ if ( ! root ) return '#' ;
38
36
39
- const { left, val, right } = root
37
+ const { left, val, right } = root ;
40
38
41
- const leftMerkle = merkle ( left )
42
- const rightMerkle = merkle ( right )
39
+ const leftMerkle = merkle ( left ) ;
40
+ const rightMerkle = merkle ( right ) ;
43
41
44
- const merkleVal = [ leftMerkle , val , rightMerkle ] . join ( '' )
45
- const merkleHash = hash ( merkleVal )
42
+ const merkleVal = [ leftMerkle , val , rightMerkle ] . join ( '' ) ;
43
+ const merkleHash = hash ( merkleVal ) ;
46
44
47
- root . merkle = merkleHash
45
+ root . merkle = merkleHash ;
48
46
49
- return root . merkle
50
- }
47
+ return root . merkle ;
48
+ } ;
51
49
52
50
const search = ( root , subRoot ) => {
53
- if ( ! root ) return false
51
+ if ( ! root ) return false ;
54
52
55
- const hasSamePath = root . merkle === subRoot . merkle
56
- if ( hasSamePath ) return true
53
+ const hasSamePath = root . merkle === subRoot . merkle ;
54
+ if ( hasSamePath ) return true ;
57
55
58
- const left = search ( root . left , subRoot )
59
- const right = search ( root . right , subRoot )
56
+ const left = search ( root . left , subRoot ) ;
57
+ const right = search ( root . right , subRoot ) ;
60
58
61
- return left || right
62
- }
59
+ return left || right ;
60
+ } ;
63
61
64
- var isSubtree = function ( root , subRoot ) {
65
- [ root , subRoot ] . forEach ( merkle )
62
+ var isSubtree = function ( root , subRoot ) {
63
+ [ root , subRoot ] . forEach ( merkle ) ;
66
64
67
- return search ( root , subRoot )
68
- }
65
+ return search ( root , subRoot ) ;
66
+ } ;
69
67
70
68
const hashify = ( root , hash , postOrderKey ) => {
71
- if ( ! root ) return '#'
69
+ if ( ! root ) return '#' ;
72
70
73
- const left = hashify ( root . left , hash , postOrderKey )
74
- const right = hashify ( root . right , hash , postOrderKey )
71
+ const left = hashify ( root . left , hash , postOrderKey ) ;
72
+ const right = hashify ( root . right , hash , postOrderKey ) ;
75
73
76
- const key = [ left , root . val , right ] . join ( '' )
74
+ const key = [ left , root . val , right ] . join ( '' ) ;
77
75
78
76
if ( ! hash . has ( key ) ) {
79
- hash . set ( key , postOrderKey [ 0 ] )
80
- postOrderKey [ 0 ] ++
77
+ hash . set ( key , postOrderKey [ 0 ] ) ;
78
+ postOrderKey [ 0 ] ++ ;
81
79
}
82
80
83
- return hash . get ( key )
84
- }
81
+ return hash . get ( key ) ;
82
+ } ;
85
83
86
- var isSubtree = function ( root , subRoot , hash = new Map ( ) , postOrderKey = [ 0 ] ) {
87
- hashify ( root , hash , postOrderKey )
84
+ var isSubtree = function ( root , subRoot , hash = new Map ( ) , postOrderKey = [ 0 ] ) {
85
+ hashify ( root , hash , postOrderKey ) ;
88
86
89
87
const hashKey = [
90
88
hashify ( subRoot . left , hash , postOrderKey ) ,
91
89
subRoot . val ,
92
- hashify ( subRoot . right , hash , postOrderKey )
93
- ] . join ( '' )
90
+ hashify ( subRoot . right , hash , postOrderKey ) ,
91
+ ] . join ( '' ) ;
92
+
93
+ return hash . has ( hashKey ) ;
94
+ } ;
94
95
95
- return hash . has ( hashKey )
96
- }
96
+ /**
97
+ * Definition for a binary tree node.
98
+ * function TreeNode(val, left, right) {
99
+ * this.val = (val===undefined ? 0 : val)
100
+ * this.left = (left===undefined ? null : left)
101
+ * this.right = (right===undefined ? null : right)
102
+ * }
103
+ */
104
+ /**
105
+ * https://leetcode.com/problems/subtree-of-another-tree/
106
+ * @param {TreeNode } root
107
+ * @param {TreeNode } subRoot
108
+ * @return {boolean }
109
+ */
110
+ var isSubtree = function ( root , subRoot ) {
111
+ if ( ! subRoot ) {
112
+ return true ;
113
+ } else if ( ! root ) {
114
+ return false ;
115
+ } else if ( isSameTree ( root , subRoot ) ) {
116
+ return true ;
117
+ }
118
+
119
+ const leftResult = isSubtree ( root . left , subRoot ) ;
120
+ const rightResult = isSubtree ( root . right , subRoot ) ;
121
+
122
+ return leftResult || rightResult ;
123
+ } ;
124
+
125
+ function isSameTree ( root , subRoot ) {
126
+ if ( ! root && ! subRoot ) {
127
+ return true ;
128
+ } else if ( ! root || ! subRoot ) {
129
+ return false ;
130
+ } else if ( root . val !== subRoot . val ) {
131
+ return false ;
132
+ }
133
+
134
+ const leftRes = isSameTree ( root . left , subRoot . left ) ;
135
+ const rightRes = isSameTree ( root . right , subRoot . right ) ;
136
+
137
+ return leftRes && rightRes ;
138
+ }
0 commit comments