2022年11月4日 星期五

[222] Count Complete Tree Nodes(TBD)

賺題數?!用recursive 寫法居然這麼簡單?!(大驚)
但是題目說 : Design an algorithm that runs in less than O(n) time complexity.
這邊完全不瞭要如何達成 XD
晚上跟室友求救一下, 先貼個pass版本
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int countNodes(struct TreeNode* root){
if (root == NULL)
return 0;
return 1+countNodes(root->left)+countNodes(root->right);
}

沒有留言:

張貼留言