LeetCode100.相同的树

题目来源:

https://leetcode-cn.com/problems/same-tree/

题目描述:

LeetCode100.相同的树_第1张图片

代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p == null || q == null) {
            return false;
        }
        return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}

 

你可能感兴趣的:(LeetCode,100.相同的树,LeetCode100)