Leetcode Algorithms-100.Same Tree

问题地址

Same Tree - LeetCode

问题描述

0.jpg

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Solution {

public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null || q == null) return false;
if (p.val != q.val) return false;
return this.isSameTree(p.left, q.left) && this.isSameTree(p.right, q.right);
}
}

class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
0%