100. Same Tree
该题是检测两棵树是否一样,主要思路是通过递归的方法对树进行中序遍历,比较简单,直接贴上代码:
1 /**
2 * Definition for a binary tree node.
3 * public class TreeNode {
4 * int val;
5 * TreeNode left;
6 * TreeNode right;
7 * TreeNode(int x) { val = x; }
8 * }
9 */
10 class Solution {
11 public boolean isSameTree(TreeNode p, TreeNode q) {
12 if( p == null && q == null ){
13 return true;
14 }else if( (p == null && q != null) || (p != null && q == null)){
15 return false;
16 }
17
18 if(p.val != q.val ){
19 return false;
20 }else{
21 return isSameTree(p.left, q.left)&&isSameTree(p.right, q.right);
22 }
23
24
25 }
26 }
END