題目:給一棵二叉樹,找出從根節點到葉子節點的全部路徑。java
思路(First Version):這道題要打印根節點到葉子節點的所有路徑, 曾經咱們有總結過,若是是輸出所有路徑的題目,那必定會想到深度搜索的方式。接下來怎麼打印呢?只須要在深度優先搜索的每一層的時候用Arraylist保存從根節點 到當前節點的全部路徑上面的點。而後判斷若是是葉子節點,那麼當前是一個可行解,保存在最後要返回的答案裏面。this
Java代碼:spa
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):.net
在構造一個二叉樹的時候遇到了新的知識:code
1,構建二叉樹要先從葉子節點開始創造,由下往上構造。blog
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){ it
paths.add(root.val + "->" + path); //依次把a裏面的元素添加到另一個paths裏面
}io
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; } }
遍歷法:for循環
紅字爲當時想錯了
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); } }