思路:

简单搜索

总结:

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;elsedfs(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: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 ...

  2. LeetCode——Maximum Depth of Binary Tree

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

  3. leetcode算法题--Binary Tree Paths

    原题链接:https://leetcode.com/problems/binary-tree-paths/ vector<string> res; vector<string> ...

  4. LeetCode 110 Balanced Binary Tree 平衡二叉树

    LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...

  5. PATH和path,傻傻分不清

    习惯了Windows电脑下的所见即所得,找到程序或文件双击即可运行或打开:于是我们被惯得以为电脑会像人一样聪明,给他一个名字就可以运行程序或打开文件:于是在命令行下或程序里不断碰壁,为啥这个命令不运行 ...

  6. LeetCode: 106. Construct Binary Tree from Inorder and Postorder Traversal

    题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...

  7. InstallShield Build Error -1014: Cannot rename directory PATH to PATH\folder.Bak.

    InstallSheild执行Build结果错误: 错误详细信息: Cannot rename directory <PATH> to <PATH>\folder.Bak. W ...

  8. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...

  9. python os path isfile_Python path.isfile方法代码示例

    本文整理汇总了Python中os.path.isfile方法的典型用法代码示例.如果您正苦于以下问题:Python path.isfile方法的具体用法?Python path.isfile怎么用?P ...

最新文章

  1. AJAX安全-Session做Token
  2. CentOS7虚拟机之间设置免密登录
  3. 知道这些面试技巧,让你的测试求职少走弯路
  4. 集群节点数和分片数关系_完全二叉树的节点数,你真的会算吗?
  5. db2增加decimal类型字段小数位_05725.16.1Hive中decimal类型字段.0结尾数据显示异常问题处理...
  6. RTSP鉴权认证之基础认证和摘要认证
  7. linux的增强文件夹,在linux系统中安装virtualbox增强功能(增强包)的详细步骤是什么...
  8. sift算法_单应性Homograph估计:从传统算法到深度学习
  9. 如何用Python进行大数据挖掘和分析
  10. 【kafka】produce response correlation id xxx on topic-partition retrying Error: NETWORK_EXCEPTION
  11. html 价格计算,HTML打折计算价格实现原理与脚本代码
  12. Layui--代码修饰器layui.code
  13. 明基电通董事长李焜耀
  14. [转载] python跨行 print:多用(),换行符\要小心,少用+或者不用(其它程序代码跨行用\就行,不能用括号)
  15. mysql 的 show profile 相关属性
  16. k touch for android,k touch for android
  17. 搞深度学习如何找到需要的代码
  18. 四方聚合支付系统+新增USDT提现+全新UI+安全升级修复XSS漏洞补单漏洞 附安装教程
  19. Hexo的入门教程及Next主题配置
  20. 弱口令扫描工具mysql ftp_超级弱口令检查工具

热门文章

  1. C#水晶报表,窗体不显示,闪退
  2. [转]Java输入输出流的使用详细介绍
  3. Spring框架之Filter应用
  4. linux 常用SHELL
  5. 永中Office—给公文设置密码
  6. Integration Services包部署常见问题汇总 (转自游子吟)
  7. MySQL的CSV引擎应用实例解析
  8. 【分享】Lucene.Net的中文分词组件AdvancedChineseAnalyzer
  9. DB2 CASE/IF 条件控制语句
  10. MESI--CPU缓存一致性协议