題目描述(中等難度)
112 題 的升級版,給定一個sum
,輸出從根節點開始到葉子節點,和爲sum
的所有路徑可能。
直接在 112 題 的基礎上改了,解法沒有新內容,大家可以過去看一看。
解法一 遞歸
112 題 的解法是下邊的樣子。
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
return hasPathSumHelper(root, sum);
}
private boolean hasPathSumHelper(TreeNode root, int sum) {
//到達葉子節點
if (root.left == null && root.right == null) {
return root.val == sum;
}
//左孩子爲 null
if (root.left == null) {
return hasPathSumHelper(root.right, sum - root.val);
}
//右孩子爲 null
if (root.right == null) {
return hasPathSumHelper(root.left, sum - root.val);
}
return hasPathSumHelper(root.left, sum - root.val) || hasPathSumHelper(root.right, sum - root.val);
}
這裏的話我們需要一個ans
變量來保存所有結果。一個temp
變量來保存遍歷的路徑。需要注意的地方就是,java
中的list
傳遞的是引用,所以遞歸結束後,要把之前加入的元素刪除,不要影響到其他分支的temp
。
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> ans = new ArrayList<>();
if (root == null) {
return ans;
}
hasPathSumHelper(root, sum, new ArrayList<Integer>(), ans);
return ans;
}
private void hasPathSumHelper(TreeNode root, int sum, ArrayList<Integer> temp, List<List<Integer>> ans) {
// 到達葉子節點
if (root.left == null && root.right == null) {
if (root.val == sum) {
temp.add(root.val);
ans.add(new ArrayList<>(temp));
temp.remove(temp.size() - 1);
}
return;
}
// 左孩子爲 null
if (root.left == null) {
temp.add(root.val);
hasPathSumHelper(root.right, sum - root.val, temp, ans);
temp.remove(temp.size() - 1);
return;
}
// 右孩子爲 null
if (root.right == null) {
temp.add(root.val);
hasPathSumHelper(root.left, sum - root.val, temp, ans);
temp.remove(temp.size() - 1);
return;
}
temp.add(root.val);
hasPathSumHelper(root.right, sum - root.val, temp, ans);
temp.remove(temp.size() - 1);
temp.add(root.val);
hasPathSumHelper(root.left, sum - root.val, temp, ans);
temp.remove(temp.size() - 1);
}
解法二 DFS 棧
112 題 中解法二講的是BFS
,但是對於這道題由於我們要保存一條一條的路徑,而BFS
是一層一層的進行的,到最後一層一次性會得到很多條路徑。這就導致遍歷過程中,我們需要很多list
來保存不同的路徑,對於這道題是不划算的。
所以這裏我們看 112 題 利用棧實現的DFS
。
看一下之前用後序遍歷實現的代碼。
public boolean hasPathSum(TreeNode root, int sum) {
List<Integer> result = new LinkedList<>();
Stack<TreeNode> toVisit = new Stack<>();
TreeNode cur = root;
TreeNode pre = null;
int curSum = 0; //記錄當前的累計的和
while (cur != null || !toVisit.isEmpty()) {
while (cur != null) {
toVisit.push(cur); // 添加根節點
curSum += cur.val;
cur = cur.left; // 遞歸添加左節點
}
cur = toVisit.peek(); // 已經訪問到最左的節點了
//判斷是否滿足條件
if (curSum == sum && cur.left == null && cur.right == null) {
return true;
}
// 在不存在右節點或者右節點已經訪問過的情況下,訪問根節點
if (cur.right == null || cur.right == pre) {
TreeNode pop = toVisit.pop();
curSum -= pop.val; //減去出棧的值
pre = cur;
cur = null;
} else {
cur = cur.right; // 右節點還沒有訪問過就先訪問右節點
}
}
return false;
}
和解法一一樣,我們需要ans
變量和temp
變量,同樣需要注意temp
是對象,是引用傳遞。
public List<List<Integer>> pathSum(TreeNode root, int sum) {
Stack<TreeNode> toVisit = new Stack<>();
List<List<Integer>> ans = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
TreeNode cur = root;
TreeNode pre = null;
int curSum = 0; // 記錄當前的累計的和
while (cur != null || !toVisit.isEmpty()) {
while (cur != null) {
toVisit.push(cur); // 添加根節點
curSum += cur.val;
/************修改的地方******************/
temp.add(cur.val);
/**************************************/
cur = cur.left; // 遞歸添加左節點
}
cur = toVisit.peek(); // 已經訪問到最左的節點了
// 判斷是否滿足條件
if (curSum == sum && cur.left == null && cur.right == null) {
/************修改的地方******************/
ans.add(new ArrayList<>(temp));
/**************************************/
}
// 在不存在右節點或者右節點已經訪問過的情況下,訪問根節點
if (cur.right == null || cur.right == pre) {
TreeNode pop = toVisit.pop();
curSum -= pop.val; // 減去出棧的值
/************修改的地方******************/
temp.remove(temp.size() - 1);
/**************************************/
pre = cur;
cur = null;
} else {
cur = cur.right; // 右節點還沒有訪問過就先訪問右節點
}
}
return ans;
}
總
和 112 題 沒什麼區別,主要是注意函數傳對象的時候,我們傳的不是對象的副本,只是傳了一個引用。
更多詳細通俗題解詳見 leetcode.wang 。