问题亟待解决:

1.一个问题一直困扰着我,想看下别人是怎么处理树的输入的,最好是以层级遍历这种清楚直观的方式。

2.关于指针*的使用

因此也导致代码不完整,没有主函数对Solution类的调用

117. Populating Next Right Pointers in Each Node II
Medium

Given a binary tree

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

 

Follow up:

  • You may only use constant extra space.
  • Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.

 

Example 1:

Input: root = [1,2,3,4,5,null,7]
Output: [1,#,2,3,#,4,5,7,#]
Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.

本题是上一题的变形,区别:
上一个完全二叉树不用区分具体树的节点是否存在,对树的遍历就有递归和非递归两种。递归方法会有不同,但非递归方法
还可以适用。非递归方法很好的一点在于,和栈的结合,将存在的元素压入栈,就忽视了树本身带来的影响。这个方法在时空
复杂度上都表现的非常好,虽然有两重循环,但循环执行的次数比较少估计是原因
#include<bits/stdc++.h>
/*
multi-line comment
*/
using namespace std;
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;
    Node* next;
//three different kinds initialize
    Node() : val(0), left(NULL), right(NULL), next(NULL) {}

    Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

    Node(int _val, Node* _left, Node* _right, Node* _next)
        : val(_val), left(_left), right(_right), next(_next) {}
};
//definition of solution
class Solution{
public:
    Node* connect(Node* root){
        //special case deal
        if(!root) return NULL;
        //define queue store list by line
        queue<Node*> q;
        q.push(root);
        //still have node not iterating
        while(!q.empty())
        {
            int len=q.size();
            for(int i=0;i<len;++i)
            {
                Node *t=q.front();
                q.pop();
                if(i<len-1) t->next=q.front();
                if(t->left) q.push(t->left);
                if(t->right) q.push(t->right);
            }
        }
        return root;
    }
};

120. Triangle
Medium

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

Note:

Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

 本题有一个思路上的指引:如何把现实中的东西变成计算机可识别的,最重要的是建立起现实世界和计算机的联系,比如本题看起来是计算机类型的,其实不是,就是二维vector,看到现实生活中的东西要想办法往计算机世界转化。
BONUS:在algorithm中有max_element方法,记得返回值需要前面加*,返回这个阶段的最大值
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;

int main()
{
    int a[10];
    for(int i=0; i<5; i++)
    {
        cin>>a[i];
    }
    cout<<*max_element(a, a+5);
    return 0;
 }

往往不具有明显判别标准的东西就是DP。所以难,因为不知道该按什么思路一步步选择

本题的思路是:

从上到下遍历然后每个节点记录当前最小值,整个最后一行就是当前所有的最小值结果,在这里选最小值。就是整体的最小值

问题:

不知道如何初始化二维vector,有段时间写代码只写class Solution,不写主函数偷懒,导致现在不太会写主函数,人生真的一步捷径都不能走。

 这段代码好在对vector的精准把握,begin end 函数,back函数 *min_element都用的很溜

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
class Solution {
public:
    //specific variable name isn't important in LC,for use many times,can limit it to a smaller one
    int minimumTotal(vector<vector<int>>& t) {
        int n=t.size();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<=i;j++)
            {
                if(i==0&&j==0) continue;
                if(j==0) t[i][j]+=t[i-1][j];
                //the else is very important to avoid special case meet the differnet if situation in the same time
                else if(j==i) t[i][j]+=t[i-1][j-1];
                else t[i][j]+=min(t[i-1][j-1],t[i-1][j]);
            }
        }
        //pay attention to the max_element usage,need begin end back means the last line in triangle
        return *min_element(begin(t.back()),end(t.back()));
    }
};

 

 今天虽然把代码量完成了,但是质量不够,有了这次的教训,以后一定要写主函数
另外复习之前的也要提上日程。
我目前的量是 2代码+6页书。坚持一段时间应该尽早加码了,不然温水煮青蛙
最后会越来越差

posted on 2019-12-08 22:02  黑暗尽头的超音速炬火  阅读(148)  评论(0)    收藏  举报