二叉树的两个节点之间最远的距离
2015-09-28 17:07
323 查看
题目
求二叉树的两个节点之间最远的距离。分析
计算一个二叉树的最大距离有两个情况:情况A: 路径经过左子树的最深节点,通过根节点,再到右子树的最深节点。
情况B: 路径不穿过根节点,而是左子树或右子树的最大距离路径,取其大者。
只需要计算这两个情况的路径距离,并取其大者,就是该二叉树的最大距离。
动态规划实现:
package java_exam; /** * @author ShaoCheng * @version 1.0 2015-9-28 */ public class DistanceOfBinaryTree { public static void main(String[] args){ // 0 // / \ // 1 2 // / \ / \ // 3 4 5 6 // / \ // 7 8 TreeNode p0 = new TreeNode(0); TreeNode p1 = new TreeNode(1); TreeNode p2 = new TreeNode(2); TreeNode p3 = new TreeNode(3); TreeNode p4 = new TreeNode(4); TreeNode p5 = new TreeNode(5); TreeNode p6 = new TreeNode(6); TreeNode p7 = new TreeNode(7); TreeNode p8 = new TreeNode(8); p0.left = p1; p0.right = p2; p1.left = p3; p1.right = p4; // p2.left = p5; // p2.right = p6; p3.left = p7; // p5.right = p8; Result res = new Result(); maxDistance(p0, res); System.out.println(res.maxDistance + " " + res.maxHeight); } public static void maxDistance(TreeNode root, Result res){ if(root == null) return; Result lhs = new Result(); Result rhs = new Result(); maxDistance(root.left, lhs); //左子树的最大高度及最远距离 maxDistance(root.right, rhs); //右子树的最大高度及最远距离 res.maxHeight = Math.max(lhs.maxHeight, rhs.maxHeight) + 1; res.maxDistance = Math.max(Math.max(lhs.maxDistance, rhs.maxDistance), lhs.maxHeight + rhs.maxHeight + 2); } private static class Result{ private int maxDistance = 0; private int maxHeight = -1; } private static class TreeNode{ private int val; private TreeNode left; private TreeNode right; TreeNode(int val){ this.val = val; } } }
参考博客
http://www.cnblogs.com/miloyip/archive/2010/02/25/1673114.html
相关文章推荐
- AVL树-自平衡二叉查找树(Java实现)
- C++动态规划之最长公子序列实例
- C++动态规划之背包问题解决方法
- C语言二叉树的非递归遍历实例分析
- 使用C语言构建基本的二叉树数据结构
- C++非递归队列实现二叉树的广度优先遍历
- C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法
- C#非递归先序遍历二叉树实例
- C++非递归建立二叉树实例
- C语言实现找出二叉树中某个值的所有路径的方法
- C++实现二叉树遍历序列的求解方法
- C#使用动态规划解决0-1背包问题实例分析
- C语言实现二叉树遍历的迭代算法
- 用C语言判断一个二叉树是否为另一个的子结构
- C++实现二叉树非递归遍历方法实例总结
- C++二叉树结构的建立与基本操作
- 深入遍历二叉树的各种操作详解(非递归遍历)
- JavaScript数据结构和算法之二叉树详解
- java使用归并删除法删除二叉树中节点的方法
- Java中二叉树数据结构的实现示例