二叉树结点定义:
Sum Root to Leaf Numbers
题目描述:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers.LeetCode
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.LeetCode
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.LeetCode
Flatten Binary Tree to Linked List
题目描述:Given a binary tree, flatten it to a linked list in-place.LeetCode
Binary Tree Maximum Path Sum
题目描述:Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.LeetCode
分析:要求二叉树中任意两个结点之间最大路径和,而不是从根结点出发到某一子结点的最大路径和。可采用深度优先遍历求解,只不过递归函数的返回值不是要求的最大和(最大和通过一个全局变量或者引用参数来同步更新),而是结点自身为根结点时到其子结点的最大路径,该值用于提供给其父结点计算最长路径(当其父节点为根结点时,下面的子结点只能是单向的)。简单来说,一个结点的最大路径和是其左子树路径和 + 右子树路径和 + 当前节点值,而返回值则是当前结点值加上左子树路径和与右子树路径和的较大值。考虑负数的影响,加入与0的比较。