543. Diameter of Binary Tree

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

Example:
Given a binary tree 

          1
         / \
        2   3
       / \     
      4   5    

 

Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].

Note: The length of path between two nodes is represented by the number of edges between them.

/* -----------------------------------
 *  WARNING:
 * -----------------------------------
 *  Your code may fail to compile
 *  because it contains public class
 *  declarations.
 *  To fix this, please remove the
 *  "public" keyword from your class
 *  declarations.
 */

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    int D;
    public int diameterOfBinaryTree(TreeNode root) {
        D = 0;
        if (root == null) return 0;
        DI(root);
        return D;        
    }
    
    public int DI(TreeNode root){
        if (root == null) return 0;
            int left = DI(root.left);
            int right = DI(root.right) ;        
              
        if (left + right > D) D = left + right;
        return Math.max(left, right) + 1 ;
               
    }
}

public class MainClass {
    public static TreeNode stringToTreeNode(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.length() == 0) {
            return null;
        }
    
        String[] parts = input.split(",");
        String item = parts[0];
        TreeNode root = new TreeNode(Integer.parseInt(item));
        Queue<TreeNode> nodeQueue = new LinkedList<>();
        nodeQueue.add(root);
    
        int index = 1;
        while(!nodeQueue.isEmpty()) {
            TreeNode node = nodeQueue.remove();
    
            if (index == parts.length) {
                break;
            }
    
            item = parts[index++];
            item = item.trim();
            if (!item.equals("null")) {
                int leftNumber = Integer.parseInt(item);
                node.left = new TreeNode(leftNumber);
                nodeQueue.add(node.left);
            }
    
            if (index == parts.length) {
                break;
            }
    
            item = parts[index++];
            item = item.trim();
            if (!item.equals("null")) {
                int rightNumber = Integer.parseInt(item);
                node.right = new TreeNode(rightNumber);
                nodeQueue.add(node.right);
            }
        }
        return root;
    }
    
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            TreeNode root = stringToTreeNode(line);
            
            int ret = new Solution().diameterOfBinaryTree(root);
            
            String out = String.valueOf(ret);
            
            System.out.print(out);
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    int D, left, right; 
    public int diameterOfBinaryTree(TreeNode root) {
        D = 0;
        if (root == null) return 0;
        DI(root);
        return D;        
    }
    
    public int DI(TreeNode root){
        if (root == null) return 0;
        int left = DI(root.left);
        int right = DI(root.right) ;        
              
        if (left + right > D) D = left + right;
        return Math.max(left, right) + 1 ;
               
    }
}



        
    

问题:

1.尽量全局变量不要和局部变量同名。

2.如果是全局变量,不要再局部方法里再次声明。

3.全局变量int left, right里, int型默认值是0。在方法DI里递归,left,right在做为全局变量的时候每次递归被重新赋值为0.

4.D 不是2 倍的 max(left, right)是因为,左子树的高度和右子树的高度不一定时一样的,如此左子树递归的次数和右子树是不一样的。2被的情况,属于一种特殊情况,即左子树和右子树高度一样。

 

 

543. Diameter of Binary Tree

 

如下的错误:

543. Diameter of Binary Tree