zoukankan      html  css  js  c++  java
  • LeetCode 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.

    算法分析:

    对根节点递归计算左右子树的Diameter,通过和类内的变量 diameter 进行比较,保存较大值。在每一次递归结束后,返回这棵子树的深度,根节点获取了左右子树的深度后,将二者相加就是该根节点下的Diameter。

    Java算法实现:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        int diameter=0;
        public int diameterOfBinaryTree(TreeNode root) {
        	diameter=0;
            getDepth(root);
            return diameter;
        }
        
        public int getDepth(TreeNode root){
        	if(root==null){
        		return -1;
        	}
        	int leftDepth=getDepth(root.left);
        	int rightDepth=getDepth(root.right);
        	int temp=leftDepth+rightDepth+2;
        	if(temp>diameter){
        		diameter=temp;
        	}
        	return Math.max(leftDepth, rightDepth)+1;
        }
    }
  • 相关阅读:
    深入浅出之正则表达式(二)
    ==和equlas的区别
    温故知新 javascript 正则表达式
    SQL Server高级内容之子查询和表链接
    C语言访问webservice小例子
    jquery菜单伸缩效果
    电影资源下载网站推荐
    百度云使用迅雷下载资源
    字符编码
    7_2 利用 const 引用避免复制
  • 原文地址:https://www.cnblogs.com/dongling/p/6579678.html
Copyright © 2011-2022 走看看