Binary Tree Paths 二叉树遍历
来源:互联网 发布:qq管家域名检测 编辑:程序博客网 时间:2024/06/06 01:41
Binary Tree Paths
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://int->string sprintf(buf, "%d", 100);//遍历的过程中记录之前的路径,一旦遍历到叶子节点便将该路径加入结果中//注意path要回溯,就相当于重新赋值 vector<string> binaryTreePaths(TreeNode* root) { vector<string> res; string path; char t[256]; if(root!=NULL) { sprintf(t,"%d",root->val); path=t; solve(res,path,root); } return res; } void solve(vector<string> &res,string &path,TreeNode *root) { if(root->left==NULL && root->right==NULL) { res.push_back(path); return ; } if(root->left) { char t[256]; sprintf(t,"%d",root->left->val); string tmp=t; string path1=path+"->"; path1=path1+tmp; solve(res,path1,root->left); } if(root->right) { char t[256]; sprintf(t,"%d",root->right->val); string tmp=t; string path2=path+"->"; path2=path2+tmp; solve(res,path2,root->right); } }};
0 0
- Binary Tree Paths 二叉树遍历
- Binary Tree Paths (二叉树路径遍历)
- 【二叉树】257. Binary Tree Paths
- 257.leetcode Binary Tree Paths(easy)[二叉树深度路径遍历]
- leetcode_257. Binary Tree Paths 二叉树深度优先遍历,深拷贝的运用
- LeetCode OJ 之 Binary Tree Paths(二叉树路径)
- [LeetCode] Binary Tree Paths - 二叉树基础系列题目
- lintcode binary-tree-paths 二叉树的所有路径
- 257. Binary Tree Paths 二叉树的路径
- Binary Tree Paths(二叉树的路径)
- 257. Binary Tree Paths(打印二叉树所有路径)
- 257. Binary Tree Paths | 二叉树的路径
- 257. Binary Tree Paths打印二叉树路径
- 【Leetcode】257 Binary Tree Paths 二叉树的路径
- 二叉树 (Binary Tree)的遍历
- leetcode -- 257. Binary Tree Paths【遍历次序】
- Tree----- 257. Binary Tree Paths(二叉树从根节点开始的所有路径)
- 二叉树非递归遍历算法Binary Tree Traverse methods
- iOS开发UI篇—iPad和iPhone开发的比较
- 斯特灵(Stirling)数
- linux常用命令(4):mkdir命令
- UVA 455 Periodic Strings
- PHP 自动加载类
- Binary Tree Paths 二叉树遍历
- 鼠标样式外边距的合并
- Java(02)-JDK 5.0 新特性
- Map有序存储数据
- mysql ERROR 1045 (28000): Access denied for user解决方法
- 伪元素
- word批量修改图片大小
- 获取ios项目名称及版本号
- iOS 下APNS推送处理函数详解