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 };

 

转载于:https://www.cnblogs.com/LCCRNblog/p/5217941.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值