257 - Binary Tree Paths && 112 - Path Sum
257 - Binary Tree Paths
#easy
Given the root
of a binary tree, return all root-to-leaf paths in any order.
A leaf is a node with no children.
Example 1:
Input: root = [1,2,3,null,5]
Output: ["1->2->5","1->3"]
Solution 1
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void traversal( TreeNode * root, vector<int> & path, vector<string> & result ) {
// 中節點
path.push_back( root -> val );
if ( root -> left == NULL && root -> right == NULL ) {
string one_path = "";
for ( int i = 0; i < path.size(); i++ ) {
if ( i == 0 ) one_path = to_string(path[i]);
else one_path += "->" + to_string(path[i]);
}
result.push_back(one_path);
return; // 回朔!
}
// 左節點
if ( root -> left ) {
traversal( root -> left, path, result );
path.pop_back();
}
// 右節點
if ( root -> right ) {
traversal( root -> right, path, result );
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<int> path;
vector<string> result;
traversal(root, path, result);
return result;
}
};
Solution 2
class Solution {
public:
void traversal( TreeNode * root, vector<int> & path, vector<string> & result ) {
path.push_back( root -> val );
if ( root -> left == NULL && root -> right == NULL ) {
string one_path = "";
for ( int i = 0; i < path.size(); i++ ) {
if ( i == 0 ) one_path = to_string(path[i]);
else one_path += "->" + to_string(path[i]);
}
result.push_back(one_path);
return;
}
if ( root -> left ) {
traversal( root -> left, path, result );
path.pop_back();
}
if ( root -> right ) {
traversal( root -> right, path, result );
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<int> path;
vector<string> result;
traversal(root, path, result);
return result;
}
};
如上代碼精簡了不少,也隱藏了不少東西。
注意在函數定義的時候void traversal(TreeNode* cur, string path, vector<string>& result)
,定義的是string path
,每次都是複製賦值,不用使用引用,否則就無法做到回溯的效果。(這裡涉及到C++語法知識)
那麼在如上代碼中,貌似沒有看到回溯的邏輯,其實不然,回溯就隱藏在traversal(cur->left, path + "->", result);
中的 path + "->"
。 每次函數調用完,path依然是沒有加上"->" 的,這就是回溯了。
112 - Path Sum
#easy
Given the root
of a binary tree and an integer targetSum
, return true
if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum
.
A leaf is a node with no children.
Example 1:
Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.
Example 2:
Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: There two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.
Example 3:
Input: root = [], targetSum = 0
Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.
Solution
class Solution {
public:
void traversal( TreeNode * root, int & path_nums, int targetSum, bool & isFound ) {
if ( root == NULL || isFound ) return;
path_nums += root -> val;
if ( root -> left == NULL && root -> right == NULL ) {
if ( path_nums == targetSum ) isFound = true;
path_nums -= root -> val;
return;
}
if ( root -> left ) {
traversal( root -> left, path_nums, targetSum, isFound );
}
if ( root -> right ) {
traversal( root -> right, path_nums, targetSum, isFound );
}
path_nums -= root -> val;
}
bool hasPathSum(TreeNode* root, int targetSum) {
int path_nums = 0;
bool isFound = false;
if ( root == NULL ) return false;
else {
traversal( root, path_nums, targetSum, isFound );
if ( isFound ) return true;
}
return false;
}
};