[leetcode] Path sum
2014-10-16 20:12
288 查看
1、题目描述
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
return true, as there exist a root-to-leaf path
is 22.
2、结题思路
其实就是树的遍历,可以采用深度优先搜索的思路。
可以使用STL中的stack,其操作:
队列queue使用的操作:
3、代码
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->2which sum
is 22.
2、结题思路
其实就是树的遍历,可以采用深度优先搜索的思路。
可以使用STL中的stack,其操作:
s.empty() 如果栈为空返回true,否则返回false s.size() 返回栈中元素的个数 s.pop() 删除栈顶元素但不返回其值 s.top() 返回栈顶的元素,但不删除该元素 s.push() 在栈顶压入新元素
队列queue使用的操作:
q.empty() 如果队列为空返回true,否则返回false q.size() 返回队列中元素的个数 q.pop() 删除队列首元素但不返回其值 q.front() 返回队首元素的值,但不删除该元素 q.push() 在队尾压入新元素 q.back() 返回队列尾元素的值,但不删除该元素
3、代码
相关文章推荐
- LeetCode Path Sum
- leetcode 112: Path Sum
- Leetcode64. Minimum Path Sum
- leetcode64. Minimum Path Sum
- leetcode [Path Sum]//待整理多种解法
- [LeetCode] Path Sum
- 【leetcode】64. Minimum Path Sum【java】
- LeetCode 112. Path Sum
- leetcode_112_Path Sum
- leetcode之 Path Sum
- 【leetcode】path sum--easy
- 【Leetcode】【Easy】Path Sum
- Java for LeetCode 112 Path Sum
- [LeetCode]112. Path Sum
- LeetCode题解:Path Sum
- 【LeetCode】64. Minimum Path Sum
- Leetcode_Path Sum
- 【leetcode刷题笔记】Path Sum
- LeetCode(124) Binary Tree Maximum Path Sum
- LeetCode 112 Path Sum