100. Same Tree

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class Solution {

    /**
     * @param TreeNode $p
     * @param TreeNode $q
     * @return Boolean
     */
    function isSameTree($p, $q) {
        // should be stopped, the node is last.
        if ($p === null && $q === null) {
            return true;
        } 
        // other node has empty
        if ($p == null || $q == null) {
            return false;
        }
        // node value is diffenerce
        if ($p->val != $q->val) {
            return false;
        }
        // search left and right
        return $this->isSameTree($p->left, $q->left) && $this->isSameTree($p->right, $q->right);
    }
}

Last updated