n刷100. Same Tree

Easy
反正不记得第几次做了,树的题除了考divide and conquer或者recursion到底还能考什么?

/**
 * 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;
        }
        if (p.val != q.val){
            return false;
        }
        if (!isSameTree(p.left, q.left)){
            return false;
        }
        if (!isSameTree(p.right, q.right)){
            return false;
        }
        return true;
    }
}

你可能感兴趣的:(n刷100. Same Tree)