p100

LeetCode 100 Same Tree 题解

1.题目:

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

题意:

判断两个数是否相等

2.解题思路:

TUT。。看代码

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
package leetcode;

import java.util.Scanner;

import tool.ListNode;
import tool.TreeNode;

public class p100 {
public static void main(String[] args) {

TreeNode p = null;
TreeNode q = null;
boolean ans = isSameTree(p, q);

}

public static boolean isSameTree(TreeNode p, TreeNode q) {

if (p == null && q == null)
return true;
if (p == null || q == null)
return false;

boolean left = isSameTree(p.left, q.left);
boolean right = isSameTree(p.right, q.right);

if (p.val == q.val && left && right) {
return true;
}
return false;
}

}



4.一些总结: