思路:

简单搜索

总结:

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的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  7. Path Sum,Path Sum II

    Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...

  8. [LeetCode]题解(python):071-Simplify Path

    题目来源: https://leetcode.com/problems/simplify-path/ 题意分析: 简化Unix上的绝对路径,也就是多个'/'代表一个,'..'表示返回上一级目录,‘.' ...

  9. LeetCode:Maximum Depth of Binary Tree_104

    LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...

随机推荐

  1. Query图像滑块插件 (支持触摸/滑动手势)

    1.http://bxslider.com/ 2.http://www.slidesjs.com/ (http://www.html5cn.org/article-6437-1.html很多歌)

  2. nginx正向代理http(一)

    nginx实现正向代理,下面以http为例说明: (1)nginx配置: server { listen 8080; resolver 114.114.114.114; access_log logs ...

  3. eclipse 安装插件的几种方式

        eclipse 安装插件的几种方式   1.使用url在线安装 ,最方便最快捷   help - install new software       接下来就是同意协议之类的,next ne ...

  4. c++友元函数之---一种特殊的友情

    类可以允许其他类或者函数访问它的私有成员,方法是令其他类或者函数成为它的友元.如果类想把一个函数或者类声明成它的友元,只需要增加一条以friend关键字开始的声明语句即可. 友元声明只能出现在类定义的 ...

  5. J2EE面试题集锦_

    一.基础问答 不能被继承的类有[Long  Double  Float  Short  Void  Class  Math  String] 他们都被final修饰         类可以被继承[Th ...

  6. 通过google浏览器的开发者工具修改cookie值

    打开一个页面F12,然后再刷新下.点到如下图位置刚可以添加或重设cookie的值.修改cookie的值时,需要注意要清除下cookie.不然修改的cookie不生效.

  7. jQuery或者js保存文件到本地

    一: // 浏览文件夹(指定文件路径) function BrowseFolder() { try { var Message = "Please select the folder pat ...

  8. 纯css实现进度条效果

    去年7月份做一个公司商城的微信页面(微信用的chrome内核)需要写一个提示返现进度的进度条效果. 一个完整的进度条效果其实可以拆分一下: 一段背景: 一小段的静态的斜纹进度条: 斜纹进度条用线性渐变 ...

  9. nfs简述

    参考:http://www.51lun-wen.cn/shenghuo/dianniaowangluo/diannaowangluo/czxt/Linux/200810/354248.html 1.什 ...

  10. [转]ListView学习笔记(一)——缓存机制

    要想优化ListView首先要了解它的工作原理,列表的显示需要三个元素:ListView.Adapter.显示的数据: 这里的Adapter就是用到了适配器模式,不管传入的是什么View在ListVi ...