编辑
2024-09-12
算法题
00
请注意,本文编写于 240 天前,最后修改于 240 天前,其中某些信息可能已经过时。

https://www.acwing.com/problem/content/35/

今天做这题

java
class Solution { public boolean hasSubtree(TreeNode pRoot1, TreeNode pRoot2) { if(pRoot1 == null || pRoot2 == null) { return false; } return v(pRoot1,pRoot2) || hasSubtree(pRoot1.left,pRoot2) || hasSubtree(pRoot1.right,pRoot2); } private boolean v(TreeNode A,TreeNode B) { if(B == null) { return true; } if(A == null || A.val != B.val){ return false; } return v(A.left,B.left) && v(A.right, B.right); } }
go
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func hasSubtree(pRoot1 *TreeNode, pRoot2 *TreeNode) bool { if(pRoot2 == nil || pRoot1 == nil) { return false; } return V(pRoot1, pRoot2) || hasSubtree(pRoot1.Left, pRoot2) || hasSubtree(pRoot1.Right, pRoot2); } func V(A, B *TreeNode) bool { if(B == nil) { return true; } if(A == nil || A.Val != B.Val) { return false; } return V(A.Left, B.Left) && V(A.Right, B.Right); }

本文作者:yowayimono

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!