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 ...
 
随机推荐
- 如何让 Qt 的程序使用 Sleep
			
Qt 为何没有提供 Sleep 论坛上不时见到有人问: Qt 为什么没有提供跨平台的 sleep 函数? 使用平台相关的 Sleep 或 nanosleep 以后,界面为什么没有反应? QThread ...
 - Python的内存管理  小理解
			
请看下面的一段代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 origin = {'a':100,'b':[1,2,34,5]} obj_copy ={}; ...
 - 基于jquery响应式网站图片无限加载瀑布流布局
			
分享一款效果非常酷的jQuery瀑布流布局无限加载图片效果.整个页面采用响应式布局,图片采用jQuery.Lazyload延时加载技术,提升整个页面的加载速度.效果图如下: 在线预览 源码下载 实 ...
 - 纯css3实现的超炫checkbox复选框和radio单选框
			
之前为大家分享了好多css3实现的按钮.今天要为大家分享的是纯css3实现的checkbox复选框和radio单选框,效果超级炫.先让我们看看图吧! 在线预览 源码下载 这个实例完全由css3实现 ...
 - slimphp中间件调用流程的理解
			
slimphp是一款微型php框架,主要是处理http请求,并调用合适的程序处理,并返回一个http响应. 它遵循php的psr7规范,可以很方便的集成其它遵循psr7规范的php组建. 当读到中间件 ...
 - python  -- 字符串和编码
			
字符串和编码 数字--文本 ascii(bg2312,shift_jis,eur_kr)--unicode--utf-8 ord(""),chr() 1 Python提供了ord ...
 - 关于Java开发过程中质量提升-1代码格式配置
			
在项目开发维护中,编码规范作为开发规范的一个组成部分,是十分重要和必须的,它不仅仅是为了提高开发效率,也有利于降低后期维护开发的成本.编码规范的根本目的就是要让不仅代码可以一目了然,也可以很容易的理解 ...
 - R语言绘制沈阳地铁线路图
			
##使用leaflet绘制地铁线路图,要求 ##(1)图中绘制地铁线路 library(dplyr) library(leaflet) library(data.table) stations< ...
 - eclipse lua
			
eclipse中的ldt插件是Lua Development Tools,开发lua专用的插件: 1.点击help->install new softWare,输入http://luaeclip ...
 - CSS 超出隐藏实现限制字数的功能代码(多浏览器支持)
			
<html> <title>css控制字数</title> <head> <style type="text/css"> ...