二叉樹所有路徑的值的和

一道很簡單的題目,這裏只是對比一下幾種思路和做法。
題目很簡單,就是一顆二叉樹由0-9數字構成,求所有root->leaf路徑組成的數的總和。

Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path1->2->3which represents the number123.
Find the total sum of all root-to-leaf numbers.
For example,
1
/ \
2 3
The root-to-leaf path1->2represents the number12.
The root-to-leaf path1->3represents the number13.
Return the sum = 12 + 13 =25.

這種dfs和bfs過程都可以求解。
我自己第一時間想到的還是dfs。
dfs有兩種典型做法,
第一種:由於root到當前節點的路徑唯一,所以可以求得當前"root->當前節點"路徑的值sum,sum傳遞給子節點,再求"root->子節點"的路徑的sum值,主要過程:包含當前節點的所有路徑值=包含左子節點所有路徑值+包含右子節點所有路徑值。
第二種:維持一個res值,當前節點的倍數=左節點的倍數10+右節點的倍數10,主要過程:dfs遍歷所有節點,res=res + currentNode.val * currentNode.multiNum。

個人倒覺得第二種思路比較直觀簡單,沒有同時向下傳遞狀態和獲得返回值兩個方向同時進行,只是單純的進行遞歸獲得返回值,直接按照第二種思路來實現(省略TreeNode的結構):

public class Solution {
    public int sumNumbers(TreeNode root) {
        int[] res=new int[1];
        res[0]=0;
        if(root==null)
            return 0;
        dfs(root,res);
        return res[0];
    }
    public int dfs(TreeNode root,int[] res){
        int k=0;
        if(root.left==null&&root.right==null)
            k=1;
        if(root.left!=null)
            k=k+10*dfs(root.left,res);
        if(root.right!=null)
            k=k+10*dfs(root.right,res);
        res[0]=res[0]+root.val*k;
        return k;
    }
}

附第一種思路的實現:

鏈接:https://www.nowcoder.com/questionTerminal/185a87cd29eb42049132aed873273e83
public class Solution {
    private int sum;
    public int sumNumbers(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        preorder(root, sb);
        return sum;
    }
     
    private void preorder(TreeNode node, StringBuilder current) {
        if (node == null) {
            return;
        }
        current.append(node.val);
        if (node.left == null && node.right == null) {
            sum += Integer.parseInt(current.toString());
        }
        preorder(node.left, current);
        preorder(node.right, current);
        current.deleteCharAt(current.length() - 1);
    }
}
public class Solution {
    public int sumNumbers(TreeNode root) {
        if(root==null)
            return 0;
         
        return helper(root,0);
    }
     
    private int helper(TreeNode root,int sum){
        if(root==null)
            return 0;
        sum=10*sum+root.val;
         
        if(root.left==null&&root.right==null)//////////必須加上,如果不加,root 處的sum 爲0!!!
              return sum;
         
        int left=helper(root.left,sum);    //
        int right=helper(root.right,sum);
         
        return left+right;
    }
}

還有回溯做法的實現:
主要過程就是維持一個StringBuilder,很典型的過程:
add current char -> next dfs -> delete current char
output string when reach end
代碼實現:

鏈接:https://www.nowcoder.com/questionTerminal/185a87cd29eb42049132aed873273e83
public class Solution {
    private int sum;
    public int sumNumbers(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        preorder(root, sb);
        return sum;
    }
     
    private void preorder(TreeNode node, StringBuilder current) {
        if (node == null) {
            return;
        }
        current.append(node.val);
        if (node.left == null && node.right == null) {
            sum += Integer.parseInt(current.toString());
        }
        preorder(node.left, current);
        preorder(node.right, current);
        current.deleteCharAt(current.length() - 1);
    }
}

比較簡單基礎的算法題,但卻可以用上很多經典的基礎算法過程。還有其它的實現,後面再補充下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章