Leetcode: mimimum depth of tree, path sum, path sum II
思路:
简单搜索
总结:
dfs 框架
1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo
2. 不需要打印路径, 可设置全局变量 ans, 在 dfs 函数中对 ans 判定, 判定的位置尽可能的多
3. 对 tree 遍历, 有两种办法, 第一种是 if(root == NULL) 第二种是 if(root->left == NULL), 我一般用第二种, 效率比较高, 但是在第二种 return 1, 第一种 return 0
4. Leetcode 给出的测试用例经常会有空的输入, 要注意
5. path sum 中树节点的 val 可以是负数, 使得剪枝变得比较困难. 这个地方 wa 过
6. path sum II 题目没要求去重, 去重的话可能比较复杂, 我暂时没想到好办法
7. vector.clear() 可以彻底清空 vector, 不需要 for 循环
代码:
minimum depth of tree
const int INF = 1E9;
class Solution {
public:
int minDepth(TreeNode *root) {
if(root == NULL)
return 0; if(root->left == NULL && root->right == NULL)
return 1; int left = INF, right = INF;
if(root->left) {
left = 1 + minDepth(root->left);
}
if(root->right)
right = 1 + minDepth(root->right); return min(left, right);
}
};
path sum
class Solution {
public:
int SUM;
bool ans;
void dfs(TreeNode *root, const int &curSum) {
if(ans)
return;
if(curSum + root->val == SUM) {
if(root->left == NULL && root->right == NULL) {
ans = true;
return;
}
}
if(root->left != NULL && !ans) {
dfs(root->left, curSum+root->val);
}
if(root->right != NULL && !ans) {
dfs(root->right, curSum+root->val);
}
}
bool hasPathSum(TreeNode *root, int sum) {
SUM = sum;
ans = false;
if(root == NULL)
return false;
else
dfs(root, 0);
return ans;
}
};
path sum II
class Solution {
public:
int SUM;
vector<vector<int> > result;
void dfs(TreeNode *root, const int &curSum, vector<int> party) {
party.push_back(root->val);
if(curSum + root->val == SUM) {
if(root->left == NULL && root->right == NULL) {
result.push_back(party);
return;
}
}
if(root->left != NULL ) {
dfs(root->left, curSum+root->val, party);
}
if(root->right != NULL ) {
dfs(root->right, curSum+root->val, party);
}
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
SUM = sum;
result.clear();
if(root != NULL) {
vector<int> temp;
dfs(root, 0, temp);
}
return result;
}
};
Leetcode: mimimum depth of tree, path sum, path sum II的更多相关文章
- 【LeetCode OJ】Binary Tree Maximum Path Sum
Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...
- LeetCode之“树”:Path Sum && Path Sum II
Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...
- Leetcode题 112 和 113. Path Sum I and II
112题目如下: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- 32. Path Sum && Path Sum II
Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- Path Sum,Path Sum II
Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...
- [LeetCode]题解(python):071-Simplify Path
题目来源: https://leetcode.com/problems/simplify-path/ 题意分析: 简化Unix上的绝对路径,也就是多个'/'代表一个,'..'表示返回上一级目录,‘.' ...
- LeetCode:Maximum Depth of Binary Tree_104
LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...
随机推荐
- 使用Scala编写Spark程序求基站下移动用户停留时长TopN
使用Scala编写Spark程序求基站下移动用户停留时长TopN 1. 需求:根据手机基站日志计算停留时长的TopN 我们的手机之所以能够实现移动通信,是因为在全国各地有许许多多的基站,只要手机一开机 ...
- u-boot中添加自定义命令
1.u-boot命令机制u-boot中,每个命令都使用一个struct cmd_tbl_s结构体定义,该定义在include/command.h中实现:struct cmd_tbl_s{ char * ...
- http,javascript的编码解码
http,javascript的编码解码 请求与响应的编码应分开分析 两者的编码,解码处理是相对独立的流程 依赖于相对独立的header: request header, response heade ...
- redis 安装 检测是否安装命令
0: 安装redis服务: # wget http://download.redis.io/releases/redis-3.2.6.tar.gz# tar xzf redis-3.2.6.tar.g ...
- Java中常见的异常__
作为一名游戏开发者,程序员,很自然必须熟悉对程序的调试方法.而要调试程序,自然需要对程序中的常见的异常有一定的了解,这些日子很多朋友都提出了很多问题,都是关于游戏中的报错,因此在这里我将一些常见的程序 ...
- tcp与http的区别
1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传输建立在“无差别”的网络之上. ...
- kubernetes生态圈
1:helm(部署工具,可以用来部署常用的应用,替代kubelet原生命令) https://github.com/kubernetes/helm 应用列表:https://github.com/ku ...
- Android——Button的颜色
.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
- Laravel Debugbar
Installation Require this package with composer: composer require barryvdh/laravel-debugbar After up ...
- i2c驱动笔记
基于bcm5300x芯片 注册平台总线设备,设备名bcm5300x_i2c,通过名称与驱动进行匹配. 注册平台总线驱动.驱动名称"bcm5300x_i2c",与设备进行匹配. dr ...