int b = 0; int e = size; int ret = nums[0]; while (b < e) { int tb = b; while (b < e && nums[b] != 0) ++b; if (b - tb <= 1) { ret = max(ret, nums[tb]); if (b < e) ret = max(ret, nums[b]); b += 1; continue; }
int sum = 1; int tb2 = tb; while (tb2 < b) sum *= nums[tb2++];
if (sum < 0) { int sumneg1 = 1; tb2 = tb; while (tb2 < b) { sumneg1 *= nums[tb2]; if (nums[tb2] < 0) break; ++tb2; } int sum1 = sumneg1 / nums[tb2] == 1 ? sumneg1 : sumneg1 / nums[tb2];
int sumneg2 = 1; tb2 = b - 1; while (tb2 >= tb) { sumneg2 *= nums[tb2]; if (nums[tb2] < 0) break; --tb2; } int sum2 = sumneg2 / nums[tb2] == 1 ? sumneg2 : sumneg2 / nums[tb2];
sum /= max(sumneg1, sumneg2); sum = max(sum, sum1); sum = max(sum, sum2); }
classSolution { public: intcountNodes(TreeNode* root){ stack<pair<TreeNode*, bool>> nodes; TreeNode* cursor = root; int ldeep = 0, rdeep = 0; while (cursor) { cursor = cursor->left; ++ldeep; } cursor = root; while (cursor) { // 1 means next is a right node nodes.push(make_pair(cursor, 1)); cursor = cursor->right; ++rdeep; } int ret = (1 << ldeep) - 1; // is a full binary tree if (ldeep == rdeep) return ret; while (!nodes.empty()) { cursor = nodes.top().first; if (!cursor->right) --ret; if (!cursor->left) --ret; if (cursor->left || cursor->right) return ret; nodes.pop(); --rdeep; // current node is right node, so next node is left node if (!nodes.empty() && nodes.top().second) { nodes.top().second = 0; nodes.push(make_pair(nodes.top().first->left, 1)); ++rdeep; continue; } // find next node with next node is right node while (!nodes.empty() && !nodes.top().second) { nodes.pop(); --rdeep; } // means some errors occur if (nodes.empty()) return-1; nodes.top().second = 0; cursor = nodes.top().first->left; while (cursor) { nodes.push(make_pair(cursor, 1)); cursor = cursor->right; ++rdeep; } // get the bottom element if (ldeep == rdeep) return ret; } // means some errors occur return-2; } };
inlineboolis_high(TreeNode* root, int path, int high){ high--; while (high >= 0) { bool flag = path & (1 << high); if (flag && root->right) { root = root->right; } elseif (!flag && root->left) { root = root->left; } else { returnfalse; } high--; }
returntrue; }
classSolution { public: intcountNodes(TreeNode* root){ if (!root) return0;
int high = 0; TreeNode* node = root->left; while (node) { high++; node = node->left; }
int first = 0; int last = 1 << high; while (first < last) { int mid = (first + last) / 2; if (is_high(root, mid, high)) { first = mid + 1; } else { last = mid; } }
return (1 << high) - 1 + last; } };
最后的 return 里面, 用 first 和 last 都可以, 我觉得 last 语义可能更好, 所以换成了 last.
on the end of return, use both first and last. i think ‘last’ more make sence, so i use ‘last’
他的计算是 full tree 节点数 + 底层节点数. 从这个算法中我学到了一些东西, 二进制和 binary tree, 真的有很大可操作空间.
the algorithm is a number of full tree nodes plus bottom nodes. I study something from the algorithm. it’s a lot of skill can use between binary system and binary tree.
在分析算法前, 我先总结一下我从这个算法中看到的规律.
before profiling this algorithm, summarize some rules of complete tree that I saw in this algorithm
首先, 将树节点排序, root 为 1, 从左到右, 从上到下, 依次递进. 得到类似的图 :
first, sort tree nodes. root is number 1, from left to right, up to down, get this map:
treat the first digit as an valid bit. 101 means right -> left -> right. get the number 13. the number sorted by bottom level is the the original number plus one.