新手算法学习之路----二叉树(所有树的路径)
题目:给一棵二叉树,找出从根节点到叶子节点的所有路径。
思路(First Version):这道题要打印根节点到叶子节点的全部路径, 曾经我们有总结过,如果是输出全部路径的题目,那一定会想到深度搜索的方式。接下来怎么打印呢?只需要在深度优先搜索的每一层的时候用Arraylist保存从根节点 到当前节点的所有路径上面的点。然后判断如果是叶子节点,那么当前是一个可行解,保存在最后要返回的答案里面。
Java代码:
public List<String> binaryTreePaths(TreeNode root) { // Write your code here List<String> r = new ArrayList<String>(); if(root == null) return r; helper(root,String.valueOf(root.val),r); return r; } public void helper(TreeNode root, String path, List<String> r){ if(root ==null) return; if(root.left == null&&root.right==null){//如果左右子树都为空的话说明为叶子节点,然后输出 r.add(path); return; } if(root.left!=null){ //root的左子树不为空的话就继续往左找,然后将左子树的值保存在path里面等到最后输出 helper(root.left, path+"->"+String.valueOf(root.left.val), r); } if(root.right!=null){ helper(root.right, path+"->"+String.valueOf(root.right.val), r); } }
思路(Second Version):
在构造一个二叉树的时候遇到了新的知识:
1,构建二叉树要先从叶子节点开始创造,由下往上构造。
2,报错No enclosing instance of type ArgPassTest is accessible. Must qualify the allocation with an enclosing instance of type ArgPassTest (e.g. x.new A() where x is an instance of ArgPassTest). 【错误原因】程序是在静态方法中直接调用动态内部类会报这样错误。 这样的错误好比类中的静态方法不能直接调用动态方法。【修改方法】可以把该内部类声明为static。 或者不要在静态方法中调用。或者把public class MyObject 改成class MyObject 并写在ArgPassTest外面。网址:http://blog.csdn.net/naruto_ahu/article/details/8079380
3,for(String path: a){
paths.add(root.val + "->" + path); //依次把a里面的元素添加到另外一个paths里面
}
package Part1; import java.util.ArrayList; import java.util.List; public class BinarryTreePath { //从底部的子节点开始分别装在每一次递归的paths里面,最后一并给根节点,其中通过两个for循环来进行的 public static List<String> binaryTreePaths(TreeNode root) { List<String> paths = new ArrayList<>(); if (root == null) { return paths; } List<String> leftPaths = binaryTreePaths(root.left); List<String> rightPaths = binaryTreePaths(root.right); for (String path : leftPaths) { paths.add(root.val + "->" + path); } for (String path : rightPaths) { paths.add(root.val + "->" + path); } // 当paths为0的时候说明是叶子节点,只需要将值附到list中即可 if (paths.size() == 0) { paths.add("" + root.val); } return paths; } public static void main(String[] args) { // TreeNode D= new TreeNode(5); TreeNode C= new TreeNode(3); TreeNode B = new TreeNode(2,D); TreeNode A = new TreeNode(1,B,C); System.out.println(binaryTreePaths(A)); } } class TreeNode { // public int val; public TreeNode left, right; public TreeNode(int val) { this.val = val; this.left = this.right = null; } public TreeNode(int val, TreeNode right){ this.val = val; this.left = null; this.right = right; } public TreeNode(int val, TreeNode left, TreeNode right){ this.val = val; this.left = left; this.right = right; } }
遍历法:
红字为当时想错了
public List<String> binaryTreePaths(TreeNode root) { List<String> paths = new ArrayList<String>(); if(root == null){ return paths; } helper(root, String.valueOf(root.val), paths); return paths; } private void helper(TreeNode root, String value, List<String> paths){ if(root == null){ return; } if(root.left== null&&root.right==null){ paths.add(value); return; } if(root.left!=null){ helper(root.left, value + "->" +String.valueOf(root.left.val),paths); } if(root.right!=null){ helper(root.right,value + "->" +String.valueOf(root.right.val),paths); } }