题解 | #牛群的相似结构#
牛群的相似结构
https://www.nowcoder.com/practice/ecaeef0d218440d295d9eff63fbc747c
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param p TreeNode类 * @param q TreeNode类 * @return bool布尔型 */ public boolean isSameTree (TreeNode p, TreeNode q) { // write code here // 只要出现节点为空,则返回布尔值,否则比较节点值并向下递归 if (p == null && q == null) { return true; } else if (p == null || q == null) { return false; } return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } }