• Leetcode题解(34)


    113. Path Sum II

    题目

    分析:

    主要考察二叉树深度优先遍历(DFS),递归调用当前节点的左右结点即可,代码如下(copy网上):

     1 /**
     2  * Definition for binary tree
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 private:
    12     vector<vector<int> > ret;
    13 public:
    14     void dfs(TreeNode *node, int sum, int curSum, vector<int> a)
    15     {
    16         if (node == NULL)
    17             return;
    18         
    19         if (node->left == NULL && node->right == NULL)
    20         {
    21             if (curSum + node->val == sum)
    22             {
    23                 a.push_back(node->val);
    24                 ret.push_back(a);
    25             }
    26             return;
    27         }
    28         
    29         a.push_back(node->val);
    30         dfs(node->left, sum, curSum + node->val, a);
    31         dfs(node->right, sum, curSum + node->val, a);
    32     }
    33     
    34     vector<vector<int> > pathSum(TreeNode *root, int sum) {
    35         // Start typing your C/C++ solution below
    36         // DO NOT write int main() function
    37         ret.clear();
    38         vector<int> a; 
    39         dfs(root, sum, 0, a);
    40         return ret;
    41     }
    42 };

    上面的方法中,采用了递归,代码简单也利于理解,如果要是不采用递归该怎么求解呢?

     ---------------------------------------------------------------------------------------------------分割线-------------------------------------------------------------------------

    116. Populating Next Right Pointers in Each Node

    题目

    分析:

    这是二叉树广度优先搜索的具体应用

    代码如下:

     1 /**
     2  * Definition for binary tree with next pointer.
     3  * struct TreeLinkNode {
     4  *  int val;
     5  *  TreeLinkNode *left, *right, *next;
     6  *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     void connect(TreeLinkNode *root) {
    12         if(NULL == root)
    13             return;
    14         
    15         queue<TreeLinkNode*> myQue;
    16         myQue.push(root);
    17         myQue.push(NULL);//每一层的末尾添加一个NULL作为挡板
    18         TreeLinkNode *temp;
    19         while(!myQue.empty())
    20         {    
    21             temp = myQue.front();
    22             myQue.pop();    
    23             if(NULL != temp)
    24             {
    25                 while(NULL != temp)
    26                 {
    27                     temp->next = myQue.front();
    28                     myQue.push(temp->left);
    29                     myQue.push(temp->right);
    30                     temp = myQue.front();
    31                     myQue.pop();
    32                 }
    33                 myQue.push(NULL);
    34             }            
    35         }        
    36     }
    37 };

     -----------------------------------------------------------------------------分割线----------------------------------------------------------------------------------------------

    121. Best Time to Buy and Sell Stock

    题目

    分析;

    题目的意思就是给定一个数组,判断两个数差值最大,但是这个最大差值有个条件限制,就是前面一个数必须必后面一个数小。

    这道题让我想起曾经看过的一道编程题,就是实现一个栈的Max函数,使得每时每刻都能获取栈中的最大值。这个函数的实现思想就是在每一层中都保存当前的最大值,如果有一个元素进栈,就和当前的最大值作比较,如果更大,就修改这个当前最大值。

    这道题的思想也差不多,可以从后往前遍历数组,就相当于“进栈”,只不过这道题没有出栈操作。

    代码如下:

     1 class Solution {
     2 public:
     3     int maxProfit(vector<int>& prices) {
     4         int size = prices.size();
     5         
     6         if(size<=1)
     7             return 0;
     8         
     9         int res=0;
    10         int currentMax=prices[size-1];
    11         
    12         for(int i=size-1-1;i>=0;i--)//从倒数第二个数开始往前迭代
    13         {
    14             if(prices[i]>currentMax)
    15             {
    16                 currentMax = prices[i];
    17             }
    18             else
    19             {
    20                 if(currentMax - prices[i] > res)
    21                     res = currentMax - prices[i];
    22             }
    23         }
    24         return res;
    25         
    26     }
    27 };
  • 相关阅读:
    目前最大的IPV6安全风险有哪些?
    PHP中exec、system等函数调用linux命令问题
    ubuntu下LAMP环境安装
    Ubuntu配置Apache虚拟主机
    XML和YAML的区别与使用方法
    使用CURL访问站点的时候出现403的解决办法
    IPv6安装及使用手册
    SuperSlide2.1-滚动
    HTML常用手册
    关于Ajax参数的思考
  • 原文地址:https://www.cnblogs.com/LCCRNblog/p/5217941.html
Copyright © 2020-2023  润新知