本文共 1337 字,大约阅读时间需要 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; }} res ? Integer.MIN_VALUE??? getMax ????????null??? 0?Math.max ??????????????????? res????????????????????????????????????????????? O(n)??????? O(h)??? n ?????h ??????
转载地址:http://emch.baihongyu.com/