zoukankan      html  css  js  c++  java
  • 【嘎】树-二叉树的直径

    leetcode 题目链接:https://leetcode-cn.com/problems/diameter-of-binary-tree

    题目: 给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过根结点。

    示例 :
    给定二叉树

    1
    /
    2 3
    /
    4 5
    返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。

    注意:两结点之间的路径长度是以它们之间边的数目表示。

    看题目理解错了,当成了一定会穿过根节点,导致一直想不通错在哪里。。。

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     public int diameterOfBinaryTree(TreeNode root) {
    12         int len = 0;
    13         if (root != null) {
    14             if (root.left != null) {
    15                 len += getLength(root.left, 1);
    16             }
    17             if (root.right != null) {
    18                 len += getLength(root.right, 1);
    19             }
    20         }
    21         return len;
    22     }
    23 
    24     public int getLength(TreeNode node, int len) {
    25         int leftlen = 0;
    26         int rightlen = 0;
    27 
    28         if (node.left != null) {
    29             leftlen = getLength(node.left, len);
    30         }
    31         if (node.right != null) {
    32             rightlen = getLength(node.right, len);
    33         }
    34         if (leftlen > rightlen) {
    35             len += leftlen;
    36         } else {
    37             len += rightlen;
    38         }
    39         return len;
    40     }
    41 
    42 }

    然后想用递归,但是return的时候只能return我想要的左子树或者右子树中的大者,还有当前最大的左子树加右子树。。陷入了。。然后评论中有人用了全局变量来存放max

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        int max = 0;
        public int diameterOfBinaryTree(TreeNode root) {
            if (root != null) {
                // 遍历每一个节点,求出此节点作为根的深度,那么左子树深度加右子树深度的最大值即是答案
                setDepth(root);
                return max;
            }
            return 0;
        }
    
        public int setDepth(TreeNode root) {
            if (root != null) {
                int left = setDepth(root.left);
                int right = setDepth(root.right);
                if (right + left > max) 
                    max = right + left;
                return Math.max(right, left) + 1;
            }
            return 0;
        }
    
    }

    1. 二叉树的直径不一定过根节点,因此需要去搜一遍以每个节点为root的子树对应的直径,取最大值。
    2. root的直径 = root左子树高度 + root右子树高度
    3. root的高度 = max {root左子树高度, root右子树高度} + 1

    越努力越幸运~ 加油ヾ(◍°∇°◍)ノ゙
  • 相关阅读:
    公布一些常用的WebServices
    ARM的嵌入式Linux应用程序开发研究
    c++读写剪贴板代码
    如何破解路由器密码(CISCO)!
    用Shell扩展实现源代码统计程序(转)
    图文例解C++类的多重继承与虚拟继承
    MSDN上关于ADO示例代码
    不错的句子
    codeforces #271(div2) F. Ant colony
    [Z]CUDA中Bank conflict冲突
  • 原文地址:https://www.cnblogs.com/utomboy/p/12453871.html
Copyright © 2011-2022 走看看