The minimum depth The maximum path sum path sum II
2016-03-29 16:01
681 查看
leetcode 上的四个问题
1 The minimum depth
2 The maximum depth
3 path sum
4 path sum II
本文一次性放到一个函数中运行:
其定义为:
The minimum depth is the number of
nodes along the shortest path from the root node down to the nearest leaf node.
The maximum depth is the number of
nodes along the longest path from the root node down to the farthest leaf node.
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.
分析
题目只要求返回{true}或者{false},因此不需要记录路径。
由于只需要求出一个结果,因此,当左、右任意一棵子树求到了满意结果,都可以及时return。
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]
]
程序如下:
#include<iostream>
#include<vector>
using namespace std;
struct TreeNode
{
int val;
TreeNode *l;
TreeNode *r;
TreeNode(int x) :val(x), l(nullptr), r(nullptr) {}
};
class Tree
{
private:
//int n;//用于计数
int n1;//要输入的节点数
TreeNode *temp[1000];
public:
TreeNode *root;
Tree()//初始化树
{
TreeNode *p;
int parent = 1, child = 0;
int str[1000];
cout << "请输入二叉树的节点个数:";
cin >> n1;
cout << "请输入节点数值:";
for (int i = 0; i < n1; i++)
cin >> str[i];
for (int i = 0; i < n1; i++)
{
p = nullptr;
if (str[i] != '\n')
{
p = new TreeNode(str[i]);
}
child++;
temp[child] = p;
if (child == 1) { root = p; }
else
{
if (p != nullptr && child % 2 == 0)
temp[parent]->l = p;
if (p != nullptr && child % 2 == 1)
temp[parent]->r = p;
if (child % 2 == 1)
{
parent++;
}
}
}
}
~Tree()
{
for (int i = 0; i < n1; i++)
{
if (temp[i] != nullptr)
delete temp[i];
}
}
};
class Solution
{
public:
//The Minimum depth
int Min_node(TreeNode *root)//寻找最小长度节点
{
return min_node(root, false);
}
int min_node(TreeNode *root, bool isleaf)
{
if (!root) return isleaf ? INT_MAX : 0;
return min(min_node(root->l, root->r != nullptr), min_node(root->r, root->l != nullptr))+1;
}
//The Maximum depth
int Max_node(TreeNode *root)//计算最长节点长度
{
if (!root) return 0;
return max(Max_node(root->l), Max_node(root->r)) + 1;
}
int min(int a, int b){ return a > b ? b : a; }
int max(int a, int b) { return a > b ? a : b; }
//path sum
bool Ispathsum(TreeNode *root, int sum)//判断是否存在这样的路径(path sum I)
{
if (root == nullptr) return false;
if (root->l == nullptr &&root->r == nullptr) return sum == root->val;
return Ispathsum(root->l, sum - root->val) || Ispathsum(root->r, sum - root->val);
}
//path su
9d6a
m II
vector<vector<int>> Pathsum(TreeNode *root,int sum)
{
vector<vector<int>>result;
vector<int>cur;
pathsum(root, sum, cur, result);
return result;
}
void pathsum(TreeNode *root, int sum, vector<int> &cur, vector<vector<int>>&result)
{
if (!root) return;
cur.push_back(root->val);
if (root->l == nullptr && root->r == nullptr)
{
if (sum == root->val)
result.push_back(cur);
}
pathsum(root->l, sum - root->val, cur, result);
pathsum(root->r, sum - root->val, cur, result);
cur.pop_back();//退出向量
}
};
int main()
{
Tree tree;
Solution s1;
vector<vector<int>>vec;
cout << "二叉树的最长深度:";
cout << s1.Max_node(tree.root);
cout << endl;
cout << "二叉树的最短深度:";
cout << s1.Min_node(tree.root);
cout << endl;
int sum;
cout << "请输入总和sum:";
cin >> sum;
cout << s1.Ispathsum(tree.root, sum) << endl;
cout << "列出满足条件的路径:";
vec = s1.Pathsum(tree.root, sum);
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[i].size(); j++)
{
cout << vec[i][j] << ",";
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}
1 The minimum depth
2 The maximum depth
3 path sum
4 path sum II
本文一次性放到一个函数中运行:
其定义为:
The minimum depth is the number of
nodes along the shortest path from the root node down to the nearest leaf node.
The maximum depth is the number of
nodes along the longest path from the root node down to the farthest leaf node.
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.
分析
题目只要求返回{true}或者{false},因此不需要记录路径。
由于只需要求出一个结果,因此,当左、右任意一棵子树求到了满意结果,都可以及时return。
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]
]
程序如下:
#include<iostream>
#include<vector>
using namespace std;
struct TreeNode
{
int val;
TreeNode *l;
TreeNode *r;
TreeNode(int x) :val(x), l(nullptr), r(nullptr) {}
};
class Tree
{
private:
//int n;//用于计数
int n1;//要输入的节点数
TreeNode *temp[1000];
public:
TreeNode *root;
Tree()//初始化树
{
TreeNode *p;
int parent = 1, child = 0;
int str[1000];
cout << "请输入二叉树的节点个数:";
cin >> n1;
cout << "请输入节点数值:";
for (int i = 0; i < n1; i++)
cin >> str[i];
for (int i = 0; i < n1; i++)
{
p = nullptr;
if (str[i] != '\n')
{
p = new TreeNode(str[i]);
}
child++;
temp[child] = p;
if (child == 1) { root = p; }
else
{
if (p != nullptr && child % 2 == 0)
temp[parent]->l = p;
if (p != nullptr && child % 2 == 1)
temp[parent]->r = p;
if (child % 2 == 1)
{
parent++;
}
}
}
}
~Tree()
{
for (int i = 0; i < n1; i++)
{
if (temp[i] != nullptr)
delete temp[i];
}
}
};
class Solution
{
public:
//The Minimum depth
int Min_node(TreeNode *root)//寻找最小长度节点
{
return min_node(root, false);
}
int min_node(TreeNode *root, bool isleaf)
{
if (!root) return isleaf ? INT_MAX : 0;
return min(min_node(root->l, root->r != nullptr), min_node(root->r, root->l != nullptr))+1;
}
//The Maximum depth
int Max_node(TreeNode *root)//计算最长节点长度
{
if (!root) return 0;
return max(Max_node(root->l), Max_node(root->r)) + 1;
}
int min(int a, int b){ return a > b ? b : a; }
int max(int a, int b) { return a > b ? a : b; }
//path sum
bool Ispathsum(TreeNode *root, int sum)//判断是否存在这样的路径(path sum I)
{
if (root == nullptr) return false;
if (root->l == nullptr &&root->r == nullptr) return sum == root->val;
return Ispathsum(root->l, sum - root->val) || Ispathsum(root->r, sum - root->val);
}
//path su
9d6a
m II
vector<vector<int>> Pathsum(TreeNode *root,int sum)
{
vector<vector<int>>result;
vector<int>cur;
pathsum(root, sum, cur, result);
return result;
}
void pathsum(TreeNode *root, int sum, vector<int> &cur, vector<vector<int>>&result)
{
if (!root) return;
cur.push_back(root->val);
if (root->l == nullptr && root->r == nullptr)
{
if (sum == root->val)
result.push_back(cur);
}
pathsum(root->l, sum - root->val, cur, result);
pathsum(root->r, sum - root->val, cur, result);
cur.pop_back();//退出向量
}
};
int main()
{
Tree tree;
Solution s1;
vector<vector<int>>vec;
cout << "二叉树的最长深度:";
cout << s1.Max_node(tree.root);
cout << endl;
cout << "二叉树的最短深度:";
cout << s1.Min_node(tree.root);
cout << endl;
int sum;
cout << "请输入总和sum:";
cin >> sum;
cout << s1.Ispathsum(tree.root, sum) << endl;
cout << "列出满足条件的路径:";
vec = s1.Pathsum(tree.root, sum);
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[i].size(); j++)
{
cout << vec[i][j] << ",";
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}
相关文章推荐
- 精华阅读第 10 期 |解开阿尔法狗(AlphaGo)人工智能的画皮
- 利用 Composer 一步一步构建自己的 PHP 框架(一)——基础准备
- VMware VirtualCenter Server Service服务启动失败
- 计算两个时间戳间隔时间, (可以用于倒计时
- (七)RxJava定时器:timer
- Android studio 制作aar 使用Gradle发布项目到JCenter仓库
- Oracle学习(四)----分组函数
- Struts2+jQuery+JSON 实现输入关键字后提示框显示数据
- Hive学习之六 《Hive进阶— —hive jdbc》 详解
- Spring MVC 笔记及简单了解
- the operation has been canceled due to restrictions in effect on this computer
- PHP的Yii框架中创建视图和渲染视图的方法详解
- 判断是主线程还是子线程
- Web Service简单例子
- 全屏js头像上传插件源码高清版
- 算法:动态规划经典题目
- C++ 隐藏 覆盖 重载
- web网站的压力测试
- Python 爬取 中关村CPU名字和主频
- 设置通知栏颜色的时候有可能导致页面的输入框弹不出来