LeetCode_binary-tree-paths
来源:互联网 发布:兵马俑知乎 编辑:程序博客网 时间:2024/05/23 12:14
原题链接:https://leetcode.com/problems/binary-tree-paths/
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ /* int i = 1000; stream << i; //将int输入流 stream >> result; //从stream中抽取前面插入的int值*/ //对递归的理解:1)不需要知道具体的细节,只需要知道其过程就行了;2)递归的调用实际上就是函数的调用,不要怕,一定能掌握class Solution {public: vector<string> binaryTreePaths(TreeNode* root) { string str=""; vector<string> path; if(!root) return path; searchPaths(path,str,root); return path; } void searchPaths(vector<string> &path,string str,TreeNode* root) { stringstream stream; if(!root->left && !root->right)//叶子节点 { stream << root->val; //stream >> str; str += stream.str();//不理解为什么要+=而不是直接= path.push_back(str); return; } else//非叶子节点 { stream << root->val; //stream >> str; str += stream.str() + "->";</span><span style="font-size:18px;">//不理解为什么要+=而不是直接=,待重新回头考虑,需要对stringstream做一定的理解 //path.push_back(str); if(root->left) searchPaths(path,str,root->left); if(root->right) searchPaths(path,str,root->right); } }};//思路:从根节点出发,往左边走,如果当前结点不是叶子节点则当前值+“->”,否则记录当前值;递归结束条件是叶子节点则返回。
0 0
- LeetCode_binary-tree-paths
- Leetcode_binary-tree-preorder-traversal
- Leetcode_binary-tree-inorder-traversal
- Leetcode_binary-tree-postorder-traversal
- LeetCode_Binary Tree Postorder Traversal
- LeetCode_Binary Tree Preorder Traversal
- leetcode_Binary Tree Preorder Traversal
- LeetCode_Binary Tree Inorder Traversal
- leetCode_Binary Search Tree Iterator
- Leetcode_Binary Tree Level Order Traversal
- LeetCode_Binary Tree Maximum Path Sum
- LeetCode_binary-tree-level-order-traversal
- leetCode_Binary Tree Maximum Path Sum
- Leetcode_binary-tree-level-order-traversal-ii
- leetcode_Binary Tree Level Order Traversal _easy
- LeetCode_Binary Tree Level Order Traversal II
- LeetCode_binary-tree-zigzag-level-order-traversal
- Leetcode_binary-tree-level-order-traversal (updated c++ and python version)
- Big Endian 和 Little Endian
- centos 启动过程
- Android:OnClickListener cannot be resolved to a type
- 九度oj 1203
- 谈谈时间函数gettimeofday
- LeetCode_binary-tree-paths
- 我的第一篇博客
- 深入分析 Java 中的中文编码问题
- 树形 DP hdu4714 Tree2cycle
- Android开发 OAuth认证
- 安装 cordova错误为权限导致
- 通过Myeclipse生成Javadoc文档的方法
- ExtJs 入门教程一[学习方法]
- Android:Plug-in com.android.ide.eclipse.adt was unable to load class android