100. 相同的树(递归)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(q == null && p == null){ return true; } else if(q == null || p == null){ return false; } else if(q.val != p.val){ return false; } else{ return isSameTree(q.left, p.left) && isSameTree(q.right, p.right); } } }
不积跬步,无以至千里;不积小流,无以成江海。