1、
Path Sum
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 <code>sum = 22</code>,
return true, as there exist a root-to-leaf path <code>5->4->11->2</code> which
sum is 22.
分析,此题比较简单,采用深度优先策略。
代码如下:
2、Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path‘s sum equals the given sum.
return
分析:此题也很简单,常见题,跟上述不同的是保存路径。
代码:
3、Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
The flattened tree should look like:
Hints:
If you notice carefully in the flattened tree, each node‘s right child points to the next node of a pre-order traversal.
分析:上述提示可以看出,相当于二叉树的先序遍历,对每一个结点,先访问根结点,再先序遍历左子树,串在根结点的右孩子上,再先序遍历原右子树,继续串在右孩子上。
4、Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
分析:此题很简单,递归。