LeetCode543. Diameter of Binary Tree
2017-10-09 15:34
295 查看
Solution1
If we think about it, there may be three conditions for the largest diameter of a binary tree.1. The largest diamater exists in the left subtree of the root.
2. The largest diameter exits in the right subtree of the root.
3. The largest diameter exists and contains the root, that is, the corresponding path of the largest diameter extends from left subtree, including root, to right subtree.
And considering the property of a binary tree, these conditions can be applied to every node in the tree. Thus we can write a helper function to deal with this question. For every node, we will return 3 values to its parent: left subtree’s max depth, right subtree’s max length, and max diameter of current tree rooted at this node.
For each tree node, max left depth, max right depth, max diameter could be calculated as following respectively:
1. max_left_depth = max(left_subtree’s max_left depth, left subtree’s max_right_depth) + 1
2. max_right_depth = max(right_subtree’s max_left depth, right subtree’s max_right_depth) + 1
3. max_diameter = max(left_subtree’s max_diameter, right_subtree’s max_diameter, max_left_subtree_depth + max_right_subtree_depth + 1)
As a result, for the root node, we just need to return the 3rd value we get since this records the maximum diameter for the whole tree. However, since the actual definition of the diameter is the number of edges, we simply deduct 1 from the value we get, which is the number of nodes in the longest diameter.
Suppose there are n nodes in the tree, this algorithm runs in
O(logn)time and
O(1)space without considering the recursion stack.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public int diameterOfBinaryTree(TreeNode root) { if (root == null) { return 0; } int ret[] = diameterHelper(root); return ret[2] - 1; } private int[] diameterHelper(TreeNode curr) { if (curr == null) return new int[]{0, 0, 0}; // [max depth of left subtree, max depth of right subtree, max diameter of left-curr-right subtree] int[] ret = new int[3]; int[] leftRet = diameterHelper(curr.left); int[] rightRet = diameterHelper(curr.right); ret[0] = Math.max(leftRet[0], leftRet[1]) + 1; ret[1] = Math.max(rightRet[0], rightRet[1]) + 1; int maxDiameter = Math.max(leftRet[0], leftRet[1]) + Math.max(rightRet[0], rightRet[1]) + 1; maxDiameter = Math.max(maxDiameter, leftRet[2]); maxDiameter = Math.max(maxDiameter, rightRet[2]); ret[2] = maxDiameter; return ret; } }
Solution2
However, from the above solution, it can be seen that the actual longest diameter for every node is just the max_left_depth + max_right_depth. Hence we could use a global max value to keep track of the largest diameter to make the code clean.class Solution { int max = 0; public int diameterOfBinaryTree(TreeNode root) { diameterHelper(root); return max; } private int diameterHelper(TreeNode root) { if (root == null) return 0; int leftDep = diameterHelper(root.left); int rightDep = diameterHelper(root.right); max = Math.max(max, leftDep + rightDep); return Math.max(leftDep, rightDep) + 1; } }
相关文章推荐
- [leetcode]@python 111. Minimum Depth of Binary Tree
- [LeetCode] 111. Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- leetcode 111. Minimum Depth of Binary Tree
- Leetcode 111,104. Minimum/Maximum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- 【LeetCode】104. Maximum Depth of Binary Tree
- [LeetCode] 111. Minimum Depth of Binary Tree
- 剑指offer 面试题19:二叉树的镜像(leetcode226. Invert Binary Tree) 题解
- [Leetcode]@python 104. Maximum Depth of Binary Tree
- LeetCode之Invert Binary Tree(Java版)
- leetcode 543. Diameter of Binary Tree
- [勇者闯LeetCode] 104. Maximum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- LeetCode-104. Maximum Depth of Binary Tree
- LeetCode 543. Diameter of Binary Tree
- leetcode 每日一题 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- 算法系列(17) Leetcode 104. Maximum Depth of Binary Tree
- leetcode 107 Binary Tree Level Order Traversal II