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 ...
随机推荐
- HDU-4848 Wow! Such Conquering! 爆搜+剪枝
Wow! Such Conquering! 题意:一个n*n的数字格,Txy表示x到y的时间.最后一行n-1个数字代表分别到2-n的最晚时间,自己在1号点,求到达这些点的时间和的最少值,如果没有满足情 ...
- 【bzoj4383】[POI2015]Pustynia 线段树优化建图+差分约束系统+拓扑排序
题目描述 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息,每条信息包含三个数l,r,k以及接下来k个正整数,表示a[l],a[l+1],...,a[r- ...
- BZOJ 2298: [HAOI2011]problem a【动态规划】
Description 一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话(可能有相同的分数) Input 第一行一个整数n,接下来n行每行两个 ...
- VB6 post图片
在VBA中怎样用XMLhttp 模拟http上传二进制文件? https://www.zhihu.com/question/40974557 作者:付杨 链接:https://www.zhihu.co ...
- Java统计程序运行时间
代码如下: 第一种是以毫秒为单位计算的. long startTime = System.currentTimeMillis(); //获取开始时间 doSomething(); //测试 ...
- LightOJ1106 Gone Fishing
Gone Fishing John is going on a fishing trip. He has h hours available, and there are n lakes in the ...
- VMware 如何通过现有虚拟机克隆新的虚拟机 (图文)
本文做的是克隆主机,并重命名主机名,做好主机名与IP的对应关系,并可以通过主机名访问对方的主机. 首先说一下克隆虚拟机的作用 克隆虚拟机的作用 因工作需要,需要用到多个虚拟机环境时,再新建几个比较麻烦 ...
- 洛谷 [P2953] 牛的数字游戏
SG搜索 n的范围在可以接受的范围内,SG搜索即可 #include <iostream> #include <cstdio> #include <cstring> ...
- Google解决跨域
1.添加 --disable-web-security --user-data-dir=D:\tmp 2.在D的根目录新建tmp文件夹
- 实现TTCP (检测TCP吞吐量)
实现TTCP (检测TCP吞吐量) 应用层协议 为了解决TCP粘包问题以及客户端阻塞问题 设计的应用层协议如下: //告知要发送的数据包个数和长度 struct SessionMessage { in ...