树- path sum
来源:互联网 发布:原声电影翻译软件 编辑:程序博客网 时间:2024/06/03 10:26
112 Path Sum33.9%Easy 剑 34
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) { if(!root) return false; if(root->val==sum&&root->left==nullptr&&root->right==nullptr) return true; return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val); } };
我的错误的代码,当输入为空时无法解决
class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(!root&&sum==0) return true; if(!root&&sum!=0) return false; return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val); } };
113. Path Sum II 与I的不同是发现所有的路径,并打印出来
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]]自己的代码: 时间超限,主要应该采用回溯的思想,不然复制过多的中间变量。
class Solution { public: vector<vector<int>> pathSum(TreeNode* root, int sum) { vector<vector<int>> res; if(!root) return res; vector<int> temp; temp.push_back(root->val); pathSum(root,sum,res,temp); return res; } void pathSum(TreeNode* root,int sum,vector<vector<int>>& res,vector<int> temp){ if(root->val==sum&&root->left==nullptr&&root->right==nullptr) { res.push_back(temp); return ; } if(root->left) pathSum(root,sum-root->val,res,temp); if(root->right) pathSum(root,sum-root->val,res,temp); return ; } };
其他好的思路: 关键是处理结束位置。
class Solution { public: vector<vector<int>> pathSum(TreeNode* root, int sum) { vector<vector<int>> res; if(!root) return res; vector<int> temp; pathSum(root,sum,res,temp); return res; } void pathSum(TreeNode* root,int sum,vector<vector<int>>& res,vector<int>& temp){//这里的temp用引用的形式,调用完成要弹出 if(root==nullptr) return ;//叶节点处理,叶节点会调用到下一层去 temp.push_back(root->val); if(root->val==sum&&root->left==nullptr&&root->right==nullptr)//若果到了叶节点,并且正好找到一个路径,那么把结果放到res中并返回。 { res.push_back(temp); temp.pop_back();//要加这一句 return ;//这里不能return,因为没有清除当前节点 } pathSum(root->left,sum-root->val,res,temp);//这里没有判断root->left是不是有效的,所以叶节点会调用到下一层 pathSum(root->right,sum-root->val,res,temp); temp.pop_back();//在叶节点的回调时,这一步把叶节点去除,返回上一层,能够保证每次加进来的节点都能够pop出去 return ; } };
437. Path Sum III 这个求路径不需要从根节点开始,只要路径的一部分是就可以
You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
Example:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 10 / \ 5 -3 / \ \ 3 2 11 / \ \3 -2 1Return 3. The paths that sum to 8 are:1. 5 -> 32. 5 -> 2 -> 13. -3 -> 11第一个解法:通用递归解法,复杂度较高
class Solution { public: int pathSum(TreeNode* root, int sum) { if(root==nullptr) return 0; int temp=0; return treePath(root,0,sum)+pathSum(root->left,sum)+pathSum(root->right,sum); return temp; } int treePath(TreeNode* root,int pre, int sum){ //求得是以当前传入节点为根节点的所有路径,注意因为后面的节点和可能出现相消的情况,所以后面要分成三分支 if(!root) return 0; int current=pre+root->val; return (current==sum)+treePath(root->left,current,sum)+treePath(root->right,current,sum); } };推荐解法:复杂度O(n) 不明白
我的代码: 出错 这个题目考察编程的基本能力和递归输入输出的处理
class Solution { public: vector<string> binaryTreePaths(TreeNode* root) { if(root==nullptr) return res; string temp; temp+=int2str(root->val); if(root->left==nullptr&&root->right==nullptr) { res.push_back(temp); return res; } if(root->left) printTreePaths(temp,root->left); if(root->right) printTreePaths(temp,root->right); return res; } void printTreePaths(string temp,TreeNode* root){ if(root->left==nullptr&&root->right==nullptr) { res.push_back(temp); return ; } temp=temp+"->"; temp=temp+int2str(root->val); if(root->left) printTreePaths(temp,root->left); if(root->right) printTreePaths(temp,root->right); return ; } string int2str(int n){ stringstream ss; ss<<n; string s; ss>>s; return s; } vector<string> res; };
正确的方法: 注意to_string这个函数
class Solution { public: vector<string> binaryTreePaths(TreeNode* root) { vector<string> res; if(!root) return res; printTree(res,root,to_string(root->val)); return res; } void printTree(vector<string>& res, TreeNode* root,string s){//注意这里的res要用引用的方式 if(!root->left&&!root->right) { res.push_back(s); return ; } if(root->left) printTree(res,root->left,s+"->"+to_string(root->left->val));//这里巧妙的使用字符串相加,直接在传参处计算,省去空间 if(root->right) printTree(res,root->right,s+"->"+to_string(root->right->val)); return ; } };
543. Diameter of Binary Tree 最长端点路径
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longestpath between any two nodes in a tree. This path may or may not pass through the root.
Example:
Given a binary tree
1 / \ 2 3 / \ 4 5
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
Note: The length of path between two nodes is represented by the number of edges between them.
寻找一个树的两个节点的最远距离分析:这道题目是最长路径的变种,只需要找到每个节点左右节点的最长路径,然后相加
class Solution { public: int diameterOfBinaryTree(TreeNode* root) {//这种方法不对,因为在子树中可能端点路径更大一些。 int sum=0; if(root==nullptr) return 0; int left=maxPath(root->left,sum); int right=maxPath(root->right,sum); return sum>left+right?sum:left+right; } int maxPath(TreeNode* root,int& sum){ if(root==nullptr) return 0; else { int left=maxPath(root->left,sum); int right=maxPath(root->right,sum); if(sum<left+right)//所以要在这里判断一下保存过程中的最大值 sum=left+right; return (left>right?left:right)+1; } } };
129. Sum Root to Leaf Numbers 思路与path sum2 完全一样
Given a binary tree containing digits from 0-9
only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3
which represents the number 123
.
Find the total sum of all root-to-leaf numbers.
For example,
1 / \ 2 3
The root-to-leaf path 1->2
represents the number 12
.
The root-to-leaf path 1->3
represents the number 13
.
Return the sum = 12 + 13 = 25
.
class Solution {public: int sumNumbers(TreeNode* root) { int sum=0; string temp; if(root==nullptr) return 0; sumNumbers(root,temp,sum); return sum; } void sumNumbers(TreeNode* root,string& temp,int&sum){ temp.push_back(static_cast<char>(root->val+'0')); if(root->left==nullptr&&root->right==nullptr) { sum+=std::stoi(temp); temp.pop_back(); return ; } if(root->left) sumNumbers(root->left,temp,sum); if(root->right) sumNumbers(root->right,temp,sum); temp.pop_back(); }};
124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
For example:
Given the below binary tree,
1 / \ 2 3
Return 6
- 树 Path Sum
- 树- path sum
- leetcode---path-sum---树
- Path Sum && Path Sum ||
- 112:Path Sum【树】【DFS】
- 二叉树 maximum path sum.
- 二叉树系列---path-sum
- 【二叉树】112. Path Sum
- leetcode---path-sum-ii---树
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- 【Ubuntu-Tensorflow】TF1.0到TF1.2出现“Key LSTM/basic_lstm_cell/bias not found in checkpoin”问题
- hadoop自定义排序对比器和分组对比器
- Struts2创建项目5--(传值)valueStack讲解
- String equals方法和==的区别
- [翻译]MapReduce: Simplified Data Processing on Large Clusters
- 树- path sum
- 凯哥
- MFC之ODBC实现对Excel文件读写
- Noip2015 子串 【动态规划】
- 我们的敏捷之路——计划会议篇
- Linux 常用命令
- (VC++)cl/nmake编译器系统环境变量配置
- JS垃圾回收机制
- Hibernate中的事务