leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree
1、
Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which
sum is 22.
分析,此题比較简单,採用深度优先策略。
代码例如以下:
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
hasPath = false;
if(root){
int tempSum = 0;
pathSumCore(root,tempSum,sum);
}
return hasPath;
}
void pathSumCore(TreeNode *root,int tempSum, int sum){
if(hasPath){
return;
}
tempSum += root->val;
if(!root->left&&!root->right){
if(tempSum == sum){
hasPath = true;
}
return;
}
if(root->left){
pathSumCore(root->left,tempSum,sum);
}
if(root->right){
pathSumCore(root->right,tempSum,sum);
}
}
bool hasPath;
};
2、Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree and sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
分析:此题也非常easy。常见题,跟上述不同的是保存路径。
代码:
class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
if(root){
int tempSum = 0;
vector<int> path;
pathSumCore(root,path,tempSum,sum);
}
return pathVec;
}
void pathSumCore(TreeNode* root,vector<int> path,int tempSum,int sum){
tempSum += root->val;
path.push_back(root->val);
if(!root->left && !root->right){
if(tempSum == sum){
pathVec.push_back(path);
}
return;
}
if(root->left){
pathSumCore(root->left,path,tempSum,sum);
}
if(root->right){
pathSumCore(root->right,path,tempSum,sum);
}
}
vector<vector<int> > pathVec;
};
3、Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
分析:上述提示能够看出,相当于二叉树的先序遍历,对每个结点,先訪问根结点,再先序遍历左子树。串在根结点的右孩子上,再先序遍历原右子树,继续串在右孩子上。
代码例如以下:
class Solution {
public:
void flatten(TreeNode *root) {
if(root){
preOrder(root);
}
}
TreeNode *preOrder(TreeNode *root){
if(!root->left && !root->right){
return root;
}
TreeNode *lastNode = NULL;
TreeNode *rightNode = root->right;
//TreeNode *leftNode = root->left;
if(root->left){
root->right = root->left;
lastNode = preOrder(root->left);
root->left = NULL;
lastNode->right = rightNode;
}
if(rightNode){
lastNode = preOrder(rightNode);
}
return lastNode;
}
};
4、Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
分析:此题非常easy,递归。
class Solution {
public:
int minDepth(TreeNode *root) {
if(!root){
return 0;
}
n_minDepth = INT_MAX;
int tempDepth = 0;
minDepthCore(root,tempDepth);
return n_minDepth;
}
void minDepthCore(TreeNode *root,int tempDepth){
++tempDepth;
if(tempDepth >= n_minDepth){
return;
}
if(!root->left && !root->right){
if(tempDepth < n_minDepth){
n_minDepth = tempDepth;
}
return;
}
if(root->left){
minDepthCore(root->left,tempDepth);
}
if(root->right){
minDepthCore(root->right,tempDepth);
}
}
int n_minDepth;
};
leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree的更多相关文章
- [Leetcode][JAVA] Path Sum I && II
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- 【leetcode】Path Sum I & II(middle)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- 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 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- C++类指针初始化
上面的代码会打印“A”. C++ 类指针定义的时候没有初始化的时候,居然可以安全的调用类内部的成员函数而不出错. 在网上查了一下: 初始化为NULL的类指针可以安全的调用不涉及类成员变量的类成员函 ...
- rsync同步命令
rsync同步时,--delete删除目标目录比源目录多余文件的方法1 .实例说明 服务器A上同步/tmp/work目录到远程服务器B的/tmp/work目录下, 同时删除B服务器/tmp/work ...
- 解决centos7中ens33中不显示IP等问题
在虚拟机中安装centos7,输入ifconfig显示command not found.在sbin目录中发现没有ifconfig文件,这是因为centos7已经不使用 ifconfig命令了,已经用 ...
- 将SSM架构中原来关于springSecurity3.x版本的写法配迁移到SpringBoot2.0框架中出现的问题解决记
迁移过程中关于这个安全框架的问题很麻烦,springBoot自带的stater中的版本是5.0,原来系统有通过实现"org.springframework.security.authenti ...
- 【BZOJ3143】【HNOI2013】游走 && 【BZOJ3270】博物馆 【高斯消元+概率期望】
刚学完 高斯消元,我们来做几道题吧! T1:[BZOJ3143][HNOI2013]游走 Description 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小 ...
- Laravel 之Cache缓存
写入缓存 Cache::put('key','value',10);//设置10分钟 获取缓存 Cache::get('key'); 增加缓存 Cache::add('key','value',10) ...
- js Math [ 随机数、绝对值、四舍五入、进一取整、舍去取整、最大值、最小值、圆周率 ]
<script> /* 数学对象:Math */ with (document) { write('<br>-3.5的绝对值:'+Math.abs(-3.5)); write( ...
- Struts2牛逼的拦截器,卧槽这才是最牛的核心!
struts 拦截器 一 拦截器简介及简单的拦截器实例 Struts2拦截器是在访问某个Action或者Action的某个方法,在字段前或者之后实施拦截,并且Struts2拦截器是可以插拔的,拦截器是 ...
- JavaSE的包装类,自动装箱和自动拆箱 ,字符窜转换,toString(),equals(), hashCode()的区别
一.基本数据类型和包装类 包装类均位于Java.lang包,包装类和基本数据类型的对应关系如下表所示: Primitive-Type Wrapper-Class byte ...
- 【深入Java虚拟机】之五:多态性实现机制——静态分派与动态分派
方法解析 Class文件的编译过程中不包含传统编译中的连接步骤,一切方法调用在Class文件里面存储的都只是符号引用,而不是方法在实际运行时内存布局中的入口地址.这个特性给Java带来了更强大的动态扩 ...