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;
    }
}
    原文作者:greatfulltime
    原文地址: https://www.jianshu.com/p/7080856cf1b6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞