Thursday, April 14, 2022

[LeetCode] 100.Same Tree

   

"踏上LeetCode解題之路,順手紀錄一下PHP練功的過程囉。這是第九篇~~"


Same Tree - LeetCode


思考過程

待補上


結果

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

Contact Form

Name

Email *

Message *