【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

解析

max ksubarray sum:  最大和 of 连续子序列 =>   最大和 of  k份连续子序列 属于dp,30行代码搞定,注意一些边界。
substr diff:  无queue的区间滑动窗口,记录当前最大值

maxtree:

九章算法中有讲到, 对每个节点,找到离他最近且比它大的左右两个节点中较小的那个,作为他的父节点。

其中,找父节点: 用递增栈分别从左向右和从右向左扫描一次,对每个节点取栈顶元素即可。 在O(n)时间里就可以找到距离该节点最近的比他大的节点。


/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * @param A: Given an integer array with no duplicates.
     * @return: The root of max tree.
     */
    TreeNode* maxTree(vector<int> A) {
        // write your code here
        int n = A.size();

        // allocate tree
        TreeNode * nodes = new TreeNode[n];
        for(int i =0; i<n; i++){
            nodes[i] = TreeNode(A[i]);
        }

        // obtain the left/right nearest larger node
        stack<int> ss;
        vector<int> lp(n, -1), rp(n, -1);
        for(int i =0; i<n; i++){
            while(!ss.empty() && A[i]>A[ss.top()]){
                ss.pop();
            }
            if(!ss.empty()){
                lp[i] = ss.top();
            }
            ss.push(i);
        }
        ss=stack<int>();
        for(int i =n-1; i>=0; i--){
            while(!ss.empty() && A[i]>A[ss.top()]){
                ss.pop();
            }
            if(!ss.empty()){
                rp[i] = ss.top();
            }
            ss.push(i);
        }

        // construct tree
        TreeNode * root = NULL;
        for(int i = 0; i<n; i++){
            if(lp[i]>=0 && (rp[i]<0 || A[rp[i]]>A[lp[i]])){
                nodes[lp[i]].right = &nodes[i];
            }
            else if(rp[i]>=0 && (lp[i]<0 || A[lp[i]]>A[rp[i]])){
                nodes[rp[i]].left =  &nodes[i];
            }else{
                root = &nodes[i];
            }
        }
        return root;
    }
};


tree iterator: 

实质:找一个节点的中序下一个节点。
用dfs(root, p) 在root树中,找p的下一个节点。 代价是O(logn)
如果要O(1)的时间代价,需要一个栈(空间O(logn))保存当前的路径(root ->xx -> p)。
/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right =
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值