/**
* Definition of SegmentTreeNode:
* class SegmentTreeNode {
* public:
* int start, end, max;
* SegmentTreeNode *left, *right;
* SegmentTreeNode(int start, int end, int max) {
* this->start = start;
* this->end = end;
* this->max = max;
* this->left = this->right = NULL;
* }
* }
*/
#define INF 0x7fffffff
class Solution {
public:
/**
*@param root, index, value: The root of segment tree and
*@ change the node's value with [index, index] to the new given value
*@return: void
*/
void modify(SegmentTreeNode *root, int index, int value) {
// write your code here
if(root == NULL) { return; }
if(root->start == root->end && root->start == index) {
root->max = value;
return;
}
int mid = (root->start + root->end) >> 1;
if(index <= mid) {
modify(root->left, index, value);
} else {
modify(root->right, index, value);
}
//root->left一定存在,root->right不一定存在。需要判斷
root->max = max(root->left->max, root->right ? root->right->max : -INF);
}
};
lintcode-Segment Tree Modify
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
推薦閱讀更多精彩內容
- 與二叉樹的中序遍歷(非遞歸)相似, 以一個數組來存儲stand-by的節點, 以root和stack來判定是否ha...
- Flatten a binary tree to a fake "linked list" in pre-orde...
- 原題 LintCode 94. Binary Tree Maximum Path Sum Description ...
- 給定一個二叉樹,找出所有路徑中各節點相加總和等于給定目標值的路徑。一個有效的路徑,指的是從根節點到葉節點的路徑。 ...
- 原題 檢查一棵二叉樹是不是完全二叉樹。完全二叉樹是指一棵樹除了最后一層,其它層上節點都有左右孩子,最后一層上,所有...