Home > Back-end >  In the 2021-02-07 s: the head of a given two binary tree node head1 and head2, how to determine whet
In the 2021-02-07 s: the head of a given two binary tree node head1 and head2, how to determine whet

Time:02-08

In the 2021-02-07 s: the head of a given two binary tree node head1 and head2, how to determine whether there is a child in the head1 tree structure and head2 are exactly the same? # # f greatly architects a daily topic

CodePudding user response:

Def isSubtree (self, s, t) :
"" "
: type s: TreeNode
T: : type TreeNode
Rtype: bool
"" "
Def isSameTree (p, q) :
If not p and not q:
Return True
Elif not p and q or (not q and p) :
Return False
The else:
If p.v al!=q.v al:
Return False
The else:
Return isSameTree (p.l eft, q.l eft) and isSameTree (p.r d.light, q.r d.light)
Prev=[s]
While prev:
The node=prev. Pop ()
If the node:
If isSameTree (node, t) :
Return True
Prev. The extend ([node. Left, node. The right])
  • Related