题解 | #树的子结构#
树的子结构
http://www.nowcoder.com/practice/6e196c44c7004d15b1610b9afca8bd88
using System;
using System.Collections.Generic;
class Solution
{
public bool HasSubtree(TreeNode pRoot1, TreeNode pRoot2)
{
// write code here
if(pRoot1==null||pRoot2==null) return false;
if(pRoot1.val==pRoot2.val)
{
if(judge(pRoot1,pRoot2))
{
return true;
}
}
return HasSubtree(pRoot1.left, pRoot2)||HasSubtree(pRoot1.right, pRoot2);
}
public bool judge(TreeNode pRoot1, TreeNode pRoot2)
{
if(pRoot2==null) return true;
if(pRoot1==null) return false;
if(pRoot1.val==pRoot2.val)
{
return judge(pRoot1.left,pRoot2.left)&&judge(pRoot1.right,pRoot2.right);
}
return false;
}
}