572.另一个树的子树

题干在此

我的解题

第一次接触二叉树,没太理解用例的结构。还是log出来后才明白结构是什么样子的,总之很垮。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var isSubtree = function(s, t) {
if(t === null) return true
if(s === null) return false
return isSame(s,t) || isSubtree(s.left,t) || isSubtree(s.right,t)

var isSame = function(a, b) {
if(b === null && a === null) return true
if(a === null || b === null) return false
if(a.val === b.val){
return same(a.left,b.left) && same(a.right,b.right)
}
return false
}
}

总结

这题感受就是二叉树都是关于递归的???

Author: Kari WanG
Link: https://mingz.wang/2020/06/04/572-另一个树的子树/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.