编程之美3.8 求二叉树中节点的最大距离

描述:如果把二叉树看成一个图,父子节点之间的连线看成双向的,定义“距离”为两个节点之间边的个数。求二叉树中相距最远的两个节点的距离。

思路:相距最远的两个节点一定是叶子节点,且这两个叶子节点的路径有两种情况:

1. 该路径经过root节点,则两个叶子节点分属root.left和root.right为根的子树,而且是两个子树中距离root.left和root.right最远的叶子节点;

2. 该路径不经过root节点,则这两个叶子节点的root.left或root.right上;

根据以上分析,参考《编程之美》给出的C语言代码,这里给出Java的实现。

public class MaxDistanceOfBinaryTree_3_8 {
    int maxLen = 0;
    public void findMaxDistance(Node root) {
        if(root == null)
            return;
        if(root.left != null) {
            findMaxDistance(root.left);
        } else {
            root.maxLeft = 0;
        }
        
        if(root.right != null) {
            findMaxDistance(root.right);
        } else {
            root.maxRight = 0;
        }
        
        if(root.left != null) {
            int tmp =0;
            if(root.left.maxLeft > root.left.maxRight) {
                tmp = root.left.maxLeft;
            } else {
                tmp = root.left.maxRight;
            }
            root.maxLeft = tmp + 1;
        }
        
        if(root.right != null) {
            int tmp = 0;
            if(root.right.maxLeft > root.right.maxRight) {
                tmp = root.right.maxLeft;
            } else {
                tmp = root.right.maxRight;
            }
            root.maxRight = tmp +1;
        }

        if(root.maxLeft + root.maxRight > maxLen) {
            maxLen = root.maxLeft + root.maxRight;
        }
    }
    
    
    public static void main(String[] args) {
        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        Node n4 = new Node(4);
        Node n5 = new Node(5);
        Node n6 = new Node(6);
        Node n7 = new Node(7);
        Node n8 = new Node(8);
        Node n9 = new Node(9);
        
        n1.left = n2; n1.right = n3;
        n2.left = n4; n2.right = n5;
        n4.left = n6;
        n5.right = n7;
        n6.left = n8;
        n7.right = n9;
        
        new MaxDistanceOfBinaryTree_3_8().findMaxDistance(n1);
    }
}

class Node {
    public int val;
    public Node left;//左子树
    public Node right;//右子树
    public int maxLeft; //左子树到叶子节点的最大距离
    public int maxRight;//右子树到叶子节点的最大距离
    public Node(int val) {
        this.val = val;
    }
}

 

上述代码在Node的定义中引入了两个额外的参数maxLeft和maxRigth,在通用的二叉树节点定义中不存在这两个参数;此外,上述代码还有一个额外的全局遍历maxLen;

处于这两个方面的考虑,修改了上述代码,并得到修改后的代码如下:

public class MaxDistanceOfBinaryTree {
    public Result findMaxDistance(Node root) {
        if(root == null)
            return new Result(0,-1);

        Result left =  findMaxDistance(root.left);
        Result right = findMaxDistance(root.right);
        
        int maxDis = left.maxDep + right.maxDep + 2;
        maxDis = Math.max(maxDis, Math.max(left.maxDis, right.maxDis));
        return new Result(maxDis, Math.max(left.maxDep, right.maxDep)+1);
    }
    
    public static void main(String[] args) {
        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        Node n4 = new Node(4);
        Node n5 = new Node(5);
        Node n6 = new Node(6);
        Node n7 = new Node(7);
        Node n8 = new Node(8);
        Node n9 = new Node(9);
        
        n1.left = n2; n1.right = n3;
        n2.left = n4; n2.right = n5;
        n4.left = n6;
        n5.right = n7;
        n6.left = n8;
        n7.right = n9;
        
        Result r = new MaxDistanceOfBinaryTree().findMaxDistance(n1);
        System.out.println(r.maxDis);
    }
}

class Result{
    public int maxDis; //以root为根的最大距离
    public int maxDep; //以root为根到叶子的最长距离
    public Result(int maxDis, int maxDep) {
        this.maxDis = maxDis;
        this.maxDep = maxDep;
    }
}

class Node {
    public int val;
    public Node left;//左子树
    public Node right;//右子树
    public Node(int val) {
        this.val = val;
    }
}

在修改后的代码中,引入了一个新的类Result,用于保存递归的root.left和root.right计算的结果。

更加详细的说明参考:http://www.cnblogs.com/miloyip/archive/2010/02/25/1673114.html

posted on 2015-04-05 18:00  linxiong1991  阅读(337)  评论(0编辑  收藏  举报

导航