LeetCode 112. Path Sum
2020-05-20 20:52:35
# leetcode
Problem
1. 题目简述
给出一棵二叉树和一个sum值,根节点为root,求从root节点到叶子节点是否存在一条路径其节点之和为sum。例如:
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.
2. 算法思路
DFS
很简单的DFS的思路,中序遍历的思路,记录从根节点到当前节点的和,到达根节点时做一次判断。
3. 解法
1 | /** |