【Leetcode】129. 求根到葉子節點數字之和
阿新 • • 發佈:2020-10-29
題目連結
https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/
題目描述
解題思路
1.先序遍歷(DFS)
2.層序遍歷(DFS)
AC程式碼
DFS解法一
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { List<String> ans = new LinkedList<>(); void preOrderTraversal(TreeNode root,StringBuffer sb1){ if(root==null) return; if(root.left == null && root.right == null){ //必須新建兩個區域性變數,這是區域性變數1。這樣在遞迴彈出函式的時候,能夠回到當初stringbuffer變數的狀態,因為StringBuffer屬於區域性變數,是每個函式棧特有的。 StringBuffer sb2 = new StringBuffer(sb1); sb2.append(root.val); String temp = sb2.toString(); ans.add(temp); return; } //必須新建兩個區域性變數,這是區域性變數2。 StringBuffer sb = new StringBuffer(sb1); sb.append(root.val); preOrderTraversal(root.left,sb); preOrderTraversal(root.right,sb); } public int stringToint(String s){ int sum = 0; for(int i = s.length()-1; i >= 0; i--){ sum += (s.charAt(i)-'0')*Math.pow(10,s.length()-1-i); } return sum; } public int sumNumbers(TreeNode root) { StringBuffer sb1 = new StringBuffer(); preOrderTraversal(root,sb1); int tot = 0; for(int i = 0; i < ans.size(); i++){ tot += stringToint(ans.get(i)); } return tot; } } //將字串轉為數字,可以不呼叫Math.pow()函式 int res = 0; for(int i = 0; i < s.length(); i++){ res = res * 10 + (s.charAt(i)-'0'); }
DFS解法二
這是官方解法,真的非常巧妙!!!!直接在遞迴的過程中就完成了計算。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public int dfs(TreeNode root, int sum){ if(root == null) return 0; sum = sum*10+root.val; if(root.left==null&&root.right==null) return sum; int leftValue = dfs(root.left,sum); int rightValue = dfs(root.right,sum); return leftValue+rightValue; } public int sumNumbers(TreeNode root) { return dfs(root,0); } }
DFS解法三
class Solution { int ans = 0; public int sumNumbers(TreeNode root) { if (root == null) return 0; int temp = 0; dfs(root, temp); return ans; } public void dfs(TreeNode root, int temp) { temp = temp * 10 + root.val; if (root.left == null && root.right == null) { ans += temp; return; } if (root.left != null) { dfs(root.left, temp); } if (root.right != null) { dfs(root.right, temp); } } } 作者:byakuya-2 連結:https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/solution/dfsyong-shi-ji-bai-100nei-cun-ji-bai-95-by-byakuya/ 來源:力扣(LeetCode) 著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。
BFS解法
class Solution {
public int sumNumbers(TreeNode root) {
if (root == null) {
return 0;
}
int sum = 0;
Queue<TreeNode> nodeQueue = new LinkedList<TreeNode>();
Queue<Integer> numQueue = new LinkedList<Integer>();
nodeQueue.offer(root);
numQueue.offer(root.val);
while (!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.poll();
int num = numQueue.poll();
TreeNode left = node.left, right = node.right;
if (left == null && right == null) {
sum += num;
} else {
if (left != null) {
nodeQueue.offer(left);
numQueue.offer(num * 10 + left.val);
}
if (right != null) {
nodeQueue.offer(right);
numQueue.offer(num * 10 + right.val);
}
}
}
return sum;
}
}
作者:LeetCode-Solution
連結:https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/solution/qiu-gen-dao-xie-zi-jie-dian-shu-zi-zhi-he-by-leetc/
來源:力扣(LeetCode)
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。