1st round, 257 Binary Tree Paths
来源:互联网 发布:学信网通知单编号 编辑:程序博客网 时间:2024/05/16 05:02
看着挺容易的,然而我对这题并没有什么印象。。。从root到leaf,那显然就是dfs了。。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { // 此题是简单题,在google面试的第二天重新振作,做的第一题。。。一次bug-free了,毕竟是easy。。。就是一道典型的dfs。。。1.31.17 public List<String> binaryTreePaths(TreeNode root) { List<String> list = new ArrayList<String>(); if(root==null) return list; addPath(root, "", list); return list; } // 这个function还是比较熟悉了,就是dfs的套路。。。 public void addPath(TreeNode root, String str, List<String> list){ if(root.left==null && root.right==null){ list.add(str+root.val); return; } if(root.left!=null){ // 在这里我还纠结了一下这 箭头是加在什么位置。。。后来想明白了。。如果有下一个,那就肯定要加箭头。。没有的话,那就自然在上面的base case 中解决了 addPath(root.left, str+root.val+"->", list); } if(root.right!=null){ addPath(root.right, str+root.val+"->", list); } }}
0 0
- 1st round, 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
- leetcode 257: Binary Tree Paths
- leetCode #257 Binary Tree Paths
- LeetCode(257)Binary Tree Paths
- [leetcode 257]Binary Tree Paths
- leetcode[257] Binary Tree Paths
- LeetCode 257: Binary Tree Paths
- leetcode 257 : Binary Tree Paths
- 【LEETCODE】257-Binary Tree Paths
- [leetcode 257] Binary Tree Paths
- LeetCode 257 Binary Tree Paths
- LeetCode 257:Binary Tree Paths
- linux driver enable dev_dbg
- Java--变量相加和常量相加(类型转换)
- 【编程之美 】-中国象棋将帅问题
- [Direct2D1.1教程] Direct2D特效概览
- 矩阵键盘中断扫描
- 1st round, 257 Binary Tree Paths
- 关于HashMap与Hashtable的一些说法
- /etc/fstab文件 详解
- HTML之SWF自适应屏幕居中显示
- JS Base64加密
- android使用友盟实现第三方登录、分享以及微信回调无反应问题解决办法
- 参数传递模拟TAB选项卡功能
- 路径用法
- 使用ProgressBar实现进度条