LeetCode 100. Same Tree
题意:给你两个二叉树,然后判定两棵树是否完全相等。
大意:用递归,先判定两个根的val值是否相等,不等就return false,相等就判定两棵树的左子树和右子树是否相等。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null || q == null) { if(p != null) return false; if(q != null) return false; return true; } if(p.val != q.val) return false; else { boolean leftIsSame = isSameTree(p.left, q.left); boolean rightIsSame = isSameTree(p.right, q.right); if(leftIsSame && rightIsSame) return true; else return false; } } }