二叉树 maximum path sum.

参考:http://blog.csdn.net/worldwindjp/article/details/18953987

Given a binary tree, find the maximum path sum.

The path may start and end at any node in the tree.
For example:
Given the below binary tree,
       1
      /  \

     2    3

Return 6.

http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/

解题报告: 返回树中任意两点路径的最大值。只要两点间有路径联通就可以。
需要注意分清最大值和递归函数的返回值。在求值函数里面不能把计算出来的最大值作为返回值返回回去,否则会有问题。

例如对于下面的这个树:

     1
   /     \

1         -3

         / 

      3

计算到-3节点时,它求出的maxSum(root.left)是3,可以用3更新max,但不能把3作为1的maxSum(root.right)返回给节点1。
如果返回给1的话,会算出最大值为4。
-3节点实际返回给1的是0。

package leetcode.binaryTree;
public class MaximumPathSum {
	// 定义了一个属性值,来存放这个最大的值。
	private Integer max =Integer.MIN_VALUE;
	public MaximumPathSum() {	
	}
	public int maxPathSum(TreeNode root){
		if(root == null)
			return 0;
		int leftMax =0;
		int rightMax =0;
		int value = root.val;
		
		if(root.left != null){
			leftMax= maxPathSum(root.left);
			if(leftMax > 0){
				value = value + leftMax;
			}	
		}
		
		if(root.right != null){
			rightMax = maxPathSum(root.right);
			if(rightMax>0){
				value = value+ rightMax;
			}
		}
		// 这里的root 是遍历到的每一个节点
		// max 存放经过root 和两侧的值
		if(value > max)
			max = value;
		// 经过root节点到单侧的最大的值
		return Math.max(root.val, Math.max(root.val + leftMax, root.val + rightMax));
	}
	
	public int maximumPathSum(TreeNode root){
		if(root == null){
			return 0;
		}
		int maxPath = maxPathSum(root);
		return Math.max(max, maxPath);
	}
}

测试用例:

package leetcode.binaryTree;
import static org.junit.Assert.*;
import org.junit.Test;
public class MaximumPathSumTest {
	@Test
	public void testMaximumPathSum() {
		TreeNode root = new TreeNode(1);
		root.left = new TreeNode(2);
		root.right = new TreeNode(2);
		root.right.left = new TreeNode(3);
		root.left.right = new TreeNode(3);
		root.left.left = new TreeNode(7);
		root.left.right.right = new TreeNode(4);
		
		MaximumPathSum mps = new MaximumPathSum();
		System.out.println(mps.maximumPathSum(root));		
	}
}


打印结果为:16


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建二叉树二叉树是一种树形结构,其中每个节点最多有两个子节点,我们可以通过递归的方式来创建一个二叉树。具体步骤如下: 首先,我们需要定义二叉树节点的结构体: ``` struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; ``` 然后,我们可以通过递归方式创建二叉树,示例代码如下: ``` TreeNode* createTree() { int val; cin >> val; // 输入节点的值 if (val == -1) { // 如果值为-1,表示该节点为空 return NULL; } TreeNode* root = new TreeNode(val); root->left = createTree(); // 递归创建左子树 root->right = createTree(); // 递归创建右子树 return root; } ``` 2. 先序遍历二叉树: 先序遍历是指先访问节点本身,再遍历其左子树和右子树。示例代码如下: ``` void preorderTraversal(TreeNode* root) { if (root == NULL) { return; } cout << root->val << " "; // 访问节点本身 preorderTraversal(root->left); // 遍历左子树 preorderTraversal(root->right); // 遍历右子树 } ``` 3. 中序遍历二叉树1: 中序遍历是指先遍历左子树,再访问节点本身,最后遍历右子树。示例代码如下: ``` void inorderTraversal1(TreeNode* root) { if (root == NULL) { return; } inorderTraversal1(root->left); // 遍历左子树 cout << root->val << " "; // 访问节点本身 inorderTraversal1(root->right); // 遍历右子树 } ``` 4. 中序遍历二叉树2: 与中序遍历1不同,这里给出一种非递归的中序遍历方法,需要使用到栈。示例代码如下: ``` void inorderTraversal2(TreeNode* root) { stack<TreeNode*> st; TreeNode* p = root; while (p != NULL || !st.empty()) { while (p != NULL) { st.push(p); p = p->left; } p = st.top(); st.pop(); cout << p->val << " "; p = p->right; } } ``` 5. 后序遍历二叉树: 后序遍历是指先遍历左子树,再遍历右子树,最后访问节点本身。示例代码如下: ``` void postorderTraversal(TreeNode* root) { if (root == NULL) { return; } postorderTraversal(root->left); // 遍历左子树 postorderTraversal(root->right); // 遍历右子树 cout << root->val << " "; // 访问节点本身 } ``` 6. 层序遍历二叉树: 层序遍历是指按照从上到下、从左到右的顺序遍历每个节点。需要使用到队列。示例代码如下: ``` void levelOrderTraversal(TreeNode* root) { if (root == NULL) { return; } queue<TreeNode*> q; q.push(root); while (!q.empty()) { TreeNode* node = q.front(); q.pop(); cout << node->val << " "; if (node->left != NULL) { q.push(node->left); } if (node->right != NULL) { q.push(node->right); } } } ``` 7. 求二叉树的深度: 二叉树的深度是指从根节点到最远叶子节点的最长路径上的节点数。可以使用递归方式求解。示例代码如下: ``` int maxDepth(TreeNode* root) { if (root == NULL) { return 0; } int leftDepth = maxDepth(root->left); int rightDepth = maxDepth(root->right); return max(leftDepth, rightDepth) + 1; } ``` 8. 退出
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值