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

click to show hints.

Hints:

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 &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. android项目引入第三方库工程出现的问题及解决方案

    一.导入libar库工程 1.使用第三方库工程libary,基本上都是从github上下载,解压后里面有个libary文件夹 2.将libary导入到eclipse中,步骤如下 1)在eclipse包 ...

  2. Android刷新页面

    代码改变世界 Android刷新页面 继承 extends Activity /*** 调用onCreate(), 目的是刷新数据,  从另一activity界面返回到该activity界面时, 此方 ...

  3. UITableView加载几种不同的cell

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  4. 分析Tapjoy的模式—分发用于ios设备的企业级应用程序

    下面简单介绍下Tapjoy的模式,供大家参考: Tapjoy最初的合作模式:“按安装奖励”(pay-per-install) Tapjoy利用非常成功的奖励性下载模式影响了App Store的免费游戏 ...

  5. BZOJ [HNOI2015]亚瑟王 ——期望DP

    发现每张卡牌最后起到作用只和是否打出去了有关. 而且每张牌打出去的概率和之前的牌打出去的情况有关. 所以我们按照牌的顺序进行DP. 然后记录$i$张牌中打出$j$张的概率,然后顺便统计答案. 直接对系 ...

  6. BZOJ 2246 [SDOI2011]迷宫探险 ——动态规划

    概率DP 记忆化搜索即可,垃圾数据,就是过不掉最后一组 只好打表 #include <cstdio> #include <cstring> #include <iostr ...

  7. 在 Linux 实例上自动安装并运行 VNC Server

    原文网址:https://help.aliyun.com/knowledge_detail/41181.html?spm=5176.8208715.110.11.4c184ae8mlC7Yy 您可以使 ...

  8. hdu 1717

    小数化分数2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  9. hdu 4882 /按排顺序使序列最优问题

    题意: 安排一个序列,该序列每个数有俩个属性:t[i].val[i].计算一个点的价值:到目前为止的总时间*val[i].,,求  安排顺序后使得 计算所有点的价值之和最小. 思路:对于任意相邻俩项, ...

  10. Hdu5921 Binary Indexed Tree

    Hdu5921 Binary Indexed Tree 思路 计数问题,题目重点在于二进制下1的次数的统计,很多题解用了数位DP来辅助计算,定义g(i)表示i的二进制中1的个数, $ans = \su ...