如果把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义"距离"为两个节点之间的个数。
写一个程序求一棵二叉树中相距最远的两个节点之间的距离。
如下图所示,粗箭头的边表示最长距离:
树中相距最远的两个节点是A, B
分析可知:对于二叉树,若要两个节点U,V相距最远,有两种情况:
1,从U节点到V节点之间的路径经过根节点
2,从U节点到V节点之间的路径不经过根节点,这种情况下,U,V节点必定在根节点的左子树或者右子树上,这样就转化为求以根节点的孩子节点为根节点的二叉树中最远的两个节点间的距离
上面所说的经过根节点,是指路径中包含根节点,例如:加入上图中只有左子树FGHA, 那么最长距离的两个节点是F, A,该路径中包含根节点F,也称为经过根节点。
于是我们可以递归求解,按照二叉树的中序遍历方式遍历二叉树,在遍历的过程中寻找相距最远的两个节点。
import java.util.ArrayList;
import java.util.List;
//二叉树中节点的最大距离
public class MaxLenInBinTree {
/*
a. 1
/ \
2 3
/ \ / \
4 5 6 7
max=4 pass "root"
b. 1
/ \
2 3
/ \
4 5
/ \
6 7
/ \
8 9
max=6. do not pass "root"
*/
public static void main(String[] args) {
int[] a={1,2,3,4,5,6,7};
//store in LevelOrder,Complete Binary Tree. 0==no child
int[] b={1,2,3,4,5,0,0,6,0,0,7,0,0,0,0,8,0,0,0,0,0,0,9};
MaxLenInBinTree m=new MaxLenInBinTree();
Node aRoot = m.createTree(a);
// Node aRoot=m.createTree(b);
m.findMaxLen(aRoot);
System.out.println(m.maxLen);
}
private int maxLen=0;
public void findMaxLen(Node node){
if(node==null) return ;
if(node.getLeft()==null){
node.setMaxLeftLen(0);
}
if(node.getRight()==null){
node.setMaxRightLen(0);
}
if(node.getLeft()!=null){
findMaxLen(node.getLeft());
}
if(node.getRight()!=null){
findMaxLen(node.getRight());
}
if(node.getLeft()!=null){
int temp=0;
Node left=node.getLeft();
if(left.getMaxLeftLen()>left.getMaxRightLen()){
temp=left.getMaxLeftLen();
}else{
temp=left.getMaxRightLen();
}
node.setMaxLeftLen(temp+1);
}
if(node.getRight()!=null){
int temp=0;
Node right=node.getRight();
if(right.getMaxLeftLen()>right.getMaxRightLen()){
temp=right.getMaxLeftLen();
}else{
temp=right.getMaxRightLen();
}
node.setMaxRightLen(temp+1);
}
if(maxLen<node.getMaxLeftLen()+node.getMaxRightLen()){
maxLen=node.getMaxLeftLen()+node.getMaxRightLen();
}
}
public Node createTree(int[] data){
List<Node> nodeList=new ArrayList<Node>();
for(int each:data){
Node n=new Node(each);
nodeList.add(n);
}
int lastRootIndex=data.length/2-1;
for(int i=0;i<=lastRootIndex;i++){
Node root=nodeList.get(i);
Node left=nodeList.get(i*2+1);
if(left.getData()!=0){
root.setLeft(left);
}else{
root.setLeft(null);
}
if(i*2+2<data.length){
Node right=nodeList.get(i*2+2);
if(right.getData()!=0){
root.setRight(right);
}else{
root.setRight(null);
}
}
}
Node root=nodeList.get(0);
return root;
}
class Node{
private int data;
private Node left;
private Node right;
private int maxLeftLen;//the max length of leftTree
private int maxRightLen;
public Node(int i){
data=i;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
this.left=null;
this.right=null;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public int getMaxLeftLen() {
return maxLeftLen;
}
public void setMaxLeftLen(int maxLeftLen) {
this.maxLeftLen = maxLeftLen;
}
public int getMaxRightLen() {
return maxRightLen;
}
public void setMaxRightLen(int maxRightLen) {
this.maxRightLen = maxRightLen;
}
}
}
转自:
http://bylijinnan.iteye.com/blog/1341532
http://blog.csdn.net/caryaliu/article/details/8107089