## 一.題目描述
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:?
Given the below binary tree and sum = 22,
~~~
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
~~~
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
## 二.題目分析
這道題屬于深度優先搜索的范疇,通過分別計算二叉樹的左右子樹是否的和是否等于sum-root->val來進行深度優先搜索,只有到達也結點搜索才結束,因此,遞歸的退出條件就是到達葉結點,同時,也要考慮輸入是空指針的情況,這種情況返回false值。同時,由于只要判斷是否存在,而不用找到每一個這樣的路徑,因此,只要左子樹滿足條件時,就可以直接返回,不需要處理右子樹,這樣就可以進行剪枝,減少計算的復雜度。
遞歸做法的時間復雜度為O(n),空間復雜度為O(logn)。
## 三.示例代碼
~~~
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
bool hasPathSum(TreeNode* root, int sum)
{
if (!root)
{
return false;
}
if (!root->left && !root->right && root->val == sum)
{
return true;
}
int SumChild = sum - root->val;
if (hasPathSum(root->left, SumChild))
{
return true;
}
if (hasPathSum(root->right, SumChild))
{
return true;
}
return false;
}
};
~~~
## 四.小結
該題屬于二叉樹深度優先搜索問題,可以通過遞歸來實現。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016騰訊軟件開發筆試題)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契數列問題)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波蘭式的計算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和問題)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching