博客
关于我
牛客网算法——名企高频面试题143题(11)
阅读量:393 次
发布时间:2019-03-04

本文共 1312 字,大约阅读时间需要 4 分钟。

???????????????????????????????????????????????????????????????

????

???????????????????????????????????????????

  • ??????????????? getMax???????????????????????
  • ?????? getMax ???????????????????????
  • ??????????????????????????????????????????????????????????????????????????
  • ????????????????????????????????????
  • ??????????????????????????
  • ????

    package ????;public class ??????? {    public class TreeNode {        int val = 0;        TreeNode left = null;        TreeNode right = null;    }    private int res = Integer.MIN_VALUE;    public int TreeMax(TreeNode root) {        if (root == null) {            return 0;        }        getMax(root);        return res;    }    private int getMax(TreeNode root) {        if (root == null) {            return 0;        }        int left = getMax(root.left);        int right = getMax(root.right);        int current = Math.max(left, right, left + right + root.val);        res = Math.max(res, current);        return current;    }}

    ????

  • TreeNode ??????????????????????????
  • TreeMax ???????????????
    • TreeMax ??????????? res ? Integer.MIN_VALUE??? getMax ????????
  • getMax ???
    • ?????????????? null??? 0?
    • ???????????????????????
    • ???????????? Math.max ??????????????????? res?
    • ???????????????????
  • ???????????????????????????????????????????? O(n)??????? O(h)??? n ?????h ??????

    转载地址:http://emch.baihongyu.com/

    你可能感兴趣的文章
    NVDIMM原理与应用之四:基于pstore 和 ramoops保存Kernel panic日志
    查看>>
    NVelocity标签使用详解
    查看>>
    NVelocity标签设置缓存的解决方案
    查看>>