![]() |
| "踏上LeetCode解題之路,順手紀錄一下PHP練功的過程囉。這是第十篇~~" |
思考過程
待補上
結果✅
/**
* 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 $root
* @return Integer
*/
function countNodes($root) {
if ($root === null) {
return 0;
}else{
$sum= 1+ $this->countNodes($root->left)+ $this->countNodes($root->right);
return $sum;
}
}
// function countNodes($root) {
// return countfuc($root);
// }
}參考內容
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int func(TreeNode *p){
if(p==NULL){
return 0;
}
else{
return 1+func(p->left)+func(p->right);
}
}
int countNodes(TreeNode* root) {
return func(root);
}
};