257. Binary Tree Paths
来源:互联网 发布:淘宝助理发货地 编辑:程序博客网 时间:2024/06/16 20:41
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"]
c++版:
void binaryTreePaths(vector<string>& result, TreeNode* root, string t) { if(!root->left && !root->right) { result.push_back(t); return; } if(root->left) binaryTreePaths(result, root->left, t + "->" + to_string(root->left->val)); if(root->right) binaryTreePaths(result, root->right, t + "->" + to_string(root->right->val));}vector<string> binaryTreePaths(TreeNode* root) { vector<string> result; if(!root) return result; binaryTreePaths(result, root, to_string(root->val)); return result;}
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ if not root: return [] return [str(root.val)+'->'+path for kid in (root.left,root.right) if kid for path in self.binaryTreePaths(kid)] or [str(root.val)]
阅读全文
0 0
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 欢迎使用CSDN-markdown编辑器
- unbuntu+win10双系统时,unbuntu调用win10的硬盘
- 在CentOS 7上安装Tensorflow
- 给定曲线方程和一点求切线方程和法线方程
- iOS监控-野指针定位
- 257. Binary Tree Paths
- Dijkstra算法
- CentOS7 如何在root设置nologin之后切换到root账户
- 常用的Linux命令
- SocketClient使用https请求连接
- 代码规范
- 抠图技术及方法简介(Image Matting Overview)
- noip 2017 d1 t2题解
- sqlServer 和mysql数据同步问题