您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: