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 ...
随机推荐
- USB设备驱动程序学习笔记(二)
一.usbmouse_as_key.c /* * drivers\hid\usbhid\usbmouse.c */ #include <linux/kernel.h>#include &l ...
- eclipse svn 冲突解决
eclipse svn 冲突解决
- C# 回调与 javascritp 回调 比较
C#: using System; using System.Collections.Generic; using System.Text; namespace Delegate { //定义委托,它 ...
- jQuery插件 -- 表单验证插件jquery.validate.js
最常使用JavaScript的场合就是表单的验证,而jQuery作为一个优秀的JavaScript库,也提供了一个优秀的表单验证插件----Validation.Validation是历史最悠久的jQ ...
- linux_UAPI_转
转自:Linux Kernel UAPI 问题描述 从3.5开始,Linux Kernel 里多了一个 uapi 文件夹,里面放了很多 Linux Kernel 各个模块的头文件.如果是第一次碰到,可 ...
- Lua 中pairs与ipairs区别
local tmp_tab = {}; tmp_tab[]="lua"; tmp_tab[]="hello" tmp_tab[]="aaa" ...
- MySQL做为手动开启事务用法
START TRANSACTION;INSERT INTO `t1` (t, t1) VALUES('124', NOW());ROLLBACK;COMMIT;
- apt-get install 的替换命令及mysql安装问题的解决
Some packages could not be installed. This may mean that you haverequested an impossible situation o ...
- Entity Framework应用:Loading Entities
Entity Framework允许控制对象之间的关系,在使用EF的过程中,很多时候我们会进行查询的操作,当我们进行查询的时候,哪些数据会被加载到内存中呢?所有的数据都需要吗?在一些场合可能有意义,例 ...
- PHP注释的艺术——phpDoc规范
用过IDE或看过其他源码的小伙伴们应该都见过类似下面这样的注释 /** * 递归获取所有游戏分类 * @param int $id * @return array */ 看得多了就大概知道了一些规 ...