思路:

简单搜索

总结:

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. codeBlocks编译undefined reference to错误

    是没有把c文件编译进去的原因. 右键项目,选择属性,弹出窗体 然后选择build targets 在最下面有个build target files:中把c文件勾选.点击ok重新编译即可. Code:: ...

  2. echarts实现动态传入数据刷新【可执行】

    <!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts</t ...

  3. git使用教程&&问题列表

    git教程[转] http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 git push ...

  4. java原生序列化和Kryo序列化性能比较

    简介 最近几年,各种新的高效序列化方式层出不穷,不断刷新序列化性能的上限,最典型的包括: 专门针对Java语言的:Kryo,FST等等 跨语言的:Protostuff,ProtoBuf,Thrift, ...

  5. 网络虚拟化(SDN,NFV..)和企业骨干网的演化

    本来昨天就规划了今天的这篇文章,无奈昨天中午自己喝了将近一瓶的52度二锅头...晚上想着今天怎么着也完了,要颓废难受一天了...没想到早上居然一点都不难受了.于是就写下了本文.正文之前,还是做个广告, ...

  6. 【WPF】ListBox无法滚动

    问题:ListBox显示多个条目时,无法滚动,也不显示滚动条. 办法: 给ListBox控件加上ScrollViewer.VerticalScrollBarVisibility和ScrollViewe ...

  7. [shell]shell脚本统计数值大小

    #! /bin/bash array=( ... ) var1= var2= ;i<=;i++)); do array[i]="$( cat /sys/bus/iio/devices/ ...

  8. java后台json如何传递到jsp中解析

    需求:  系统前端jsp使用的是easyUi的datagrid展示了一些任务信息,任务信息中有个状态信息显示的值是数字, 需要根据后台保存的映射关系,将状态显示为描述信息. 原来的jsp前端显示: 解 ...

  9. Java-jdbc工具类DBUtils

    创建项目: 导入相应jar包: 看上图. JDBCUtil.java获取数据库连接文件: package com.gordon.jdbcutil; import java.io.InputStream ...

  10. Comet——随着AJAX技术兴起而产生的新技术

    不得不说Ajax确实是一个好东西,由它的出现使得WEB端新技术不断产生,Comet就属于这么一个技术,这个技术有时叫做反向AJAX,有时叫做服务器"推"技术,嗯,不要被牛逼闪闪的名 ...