思路:

简单搜索

总结:

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. electron 的跳转

    // 测试 正常跳转应该登录成功 // that.timer = setInterval(() => { that.$router.push('/mainChat');//路由跳转mainCha ...

  2. Redis Keys 命令 - 查找所有符合给定模式( pattern)的 key

    Redis Keys 命令用于查找所有符合给定模式 pattern 的 key .. 语法 redis KEYS 命令基本语法如下: redis 127.0.0.1:6379> KEYS PAT ...

  3. javascript 中解析json

    首先温习一下JSON格式: (1) 单个对象{"变量名1":"变量值1","变量名2":"变量值2"} (2) 数组[{ ...

  4. 如何用Visual Studio 2013 (vs2013)编写C语言程序

    如何用Visual Studio 2013 (vs2013)编写C语言程序 (2014-05-16 10:58:15)   Visual Studio 2013是一个很强大的软件,但是刚开始用Visu ...

  5. jsp报源码

    刚在get的一个姿势.在参数后面加负号即爆出源码. w7oami 表哥解释道其原理如下: 1.用了@file_get_contents 函数 2.cdn 或者负载均衡 才导致爆出源码.

  6. thrift与protobuffer的区别

    thrift由facebook出品,protobuffer由google出品: 下面对比一下这两个的区别.参考:http://zhidao.baidu.com/link?url=yNLBeHhWokf ...

  7. linux中断编程

    本文档只介绍中断编程所需的函数及应用,中断完整处理流程应参考文档<linux中断处理流程>,可参考文档<linux内核对中断的处理方式>对中断初步了解. 本文档基于3.14内核 ...

  8. es 配置文件

    [root@es02 config]# egrep -v "^(#|$)" elasticsearch.yml cluster.name: v5-applicationnode.n ...

  9. iOS开发小技巧--定义宏和pch文件的使用

    一.创建pch文件,默认跟项目同名 二.告诉系统,编译的时候要编译pch文件的步骤 三.把经常用到的宏  或者  分类 包含到这里

  10. java-jsp与servlet

    Java Servlet 是运行在带有支持 Java Servlet 规范的解释器的 web 服务器上的 Java 类. 每次服务器接收到一个 Servlet 请求时,服务器会产生一个新的线程并调用服 ...