Binary Tree Paths
来源:互联网 发布:阿里云的产品好吗 编辑:程序博客网 时间:2024/06/06 08:52
这也成了基本功。。。。。。。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> results = new ArrayList<String>(); if (root == null) { return results; } helper(results, String.valueOf(root.val), root); return results; } private void helper(List<String> results, String path, TreeNode root) { if (root == null) { return; } if (root.left == null && root.right == null) { results.add(path); return; } if (root.left != null) { helper(results, path + "->" + String.valueOf(root.left.val), root.left); } if (root.right != null) { helper(results, path + "->" + String.valueOf(root.right.val), root.right); } } // public List<String> binaryTreePaths(TreeNode root) { // List<String> results = new ArrayList<String>(); // if (root == null) { // return results; // } // //List<String> list = new ArrayList<String>(); // StringBuilder sb = new StringBuilder(); // helper(results, sb.append(root.val + "->"), root); // return results; // } // private void helper(List<String> results, StringBuilder sb, TreeNode root) { // if (root == null) { // return; // } // if (root.left == null && root.right == null) { // results.add(sb.substring(0, sb.length() - 2); // return; // } // helper(results, sb.a, root.left); // //list.remove(list.size() - 1); // helper(results, sb, root.right); // //list.remove(list.size() - 1); // }}
0 0
- Binary Tree Paths
- Leetcode: Binary Tree Paths
- [LeetCode] Binary Tree Paths
- [Leetcode]Binary Tree Paths
- leetcode:Binary Tree Paths
- Binary Tree Paths
- [leetcode] Binary Tree Paths
- 257 Binary Tree Paths
- Binary Tree Paths
- [LeetCode257] Binary Tree Paths
- Binary Tree Paths
- Binary Tree Paths
- Leetcode: Binary Tree Paths
- LeetCode:Binary Tree Paths
- leetcode Binary Tree Paths
- leetcode Binary Tree Paths
- LeetCode:Binary Tree Paths
- Binary Tree Paths
- 关于android资源文件那些事。
- Eclipse下用javadoc导出Android项目的帮助文档
- 平面扫描思想在ACM竞赛中的应用(转载)
- UNIX 环境高级编程(五)—— unistd.h
- linux下supervisord的简单使用
- Binary Tree Paths
- centos环境下配置多个版本的mysql
- 类的定义和实例化
- ubuntu 15.10 更改设备网卡名称为eth0 网卡
- Linux查看Nginx或者其他程序的安装目录
- iOS开发手机号判断(截止到2016年5月的所有号段)
- ASIHTTPRequest介绍
- jxl导入导出Excel
- 保存Bitmap到本地文件夹