257.leetcode Binary Tree Paths(easy)[二叉树深度路径遍历]
来源:互联网 发布:nba直播数据统计 编辑:程序博客网 时间:2024/05/18 01:34
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1 / \2 3 \ 5
All root-to-leaf paths are:
["1->2->5", "1->3"]
具体思路:要深度优先遍历二叉树,那么一般都用到栈,这里用函数的栈递归来完成
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: void getResult(vector<string> &result,TreeNode* root,string &s) { stringstream ss; ss<<root->val; string temp = s; temp += ss.str(); string s1("->"); int flag1 = 0,flag2 = 0; if(root->left != NULL) { temp += s1; flag1 = 1; getResult(result,root->left,temp); } if(root->right != NULL) { if(!flag1) temp += s1; flag2 = 1; getResult(result,root->right,temp); } if(!flag1&&!flag2)//左右子树都为空,即叶子节点的时候一条路径完成 { result.push_back(temp); } } vector<string> binaryTreePaths(TreeNode* root) { vector<string> result; if(root == NULL) return result; string s; getResult(result,root,s); return result; }};
0 0
- 257.leetcode Binary Tree Paths(easy)[二叉树深度路径遍历]
- Binary Tree Paths (二叉树路径遍历)
- leetcode 257. Binary Tree Paths-树路径|深度搜索
- leetcode 257. Binary Tree Paths 深度优先遍历DFS
- LeetCode OJ 之 Binary Tree Paths(二叉树路径)
- 【Leetcode】257 Binary Tree Paths 二叉树的路径
- Leetcode 257. Binary Tree Paths (Easy) (cpp)
- Binary Tree Paths 二叉树遍历
- 257. Binary Tree Paths 二叉树的路径
- 257. Binary Tree Paths(打印二叉树所有路径)
- 257. Binary Tree Paths | 二叉树的路径
- 257. Binary Tree Paths打印二叉树路径
- leetcode 110. Balanced Binary Tree-平衡二叉树|深度遍历
- leetcode -- 257. Binary Tree Paths【遍历次序】
- leetcode_257. Binary Tree Paths 二叉树深度优先遍历,深拷贝的运用
- 145.leetcode Binary Tree Postorder Traversal(easy)[二叉树后序遍历]
- LeetCode解题报告 257. Binary Tree Paths [easy]
- leetcode 257[easy]-----Binary Tree Paths
- js原生rotate函数
- abababababababababababababababababababababababababababababababababababababababababababababababababab
- Divisibility
- HDOJ 1290 献给杭电五十周年校庆的礼物
- Minimum Depth of Binary Tree
- 257.leetcode Binary Tree Paths(easy)[二叉树深度路径遍历]
- 设计模式---行为类型---解释器
- 【 CodeForces 612A 】 The Text Splitting
- 水仙花数——java
- 自定义View之利用组合View实现复用
- mysql处理字符串的两个绝招:substring_index,concat
- A - Divisibility
- Lucene搜索引擎入门写法
- HDU 1850 Being a Good Boy in Spring Festival【标准Nim博弈,先手赢的方案数】