Skip to content

Commit 25e3785

Browse files
committed
solve problem Diameter Of Binary Tree
1 parent cda4e6e commit 25e3785

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ All solutions will be accepted!
144144
|628|[Maximum Product Of Three Numbers](https://leetcode-cn.com/problems/maximum-product-of-three-numbers/description/)|[java/py/js](./algorithms/MaximumProductOfThreeNumbers)|Easy|
145145
|724|[Find Pivot Index](https://leetcode-cn.com/problems/find-pivot-index/description/)|[java/py/js](./algorithms/FindPivotIndex)|Easy|
146146
|53|[Maximum Subarray](https://leetcode-cn.com/problems/maximum-subarray/description/)|[java/py/js](./algorithms/MaximumSubarray)|Easy|
147+
|543|[Diameter Of Binary](https://leetcode-cn.com/problems/diameter-of-binary-tree/description/)|[java/py/js](./algorithms/DiameterOfBinary)|Easy|
147148

148149
# Database
149150
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Diameter Of Binary Tree
2+
This problem is easy to solve by postorder traversal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 diameter = 0;
13+
Map<TreeNode, Integer> pathLengthMap = new HashMap<TreeNode, Integer>();
14+
List<TreeNode> stack = new ArrayList<TreeNode>();
15+
16+
if (root != null) stack.add(root);
17+
18+
while (stack.size() > 0) {
19+
TreeNode node = stack.get(stack.size() - 1);
20+
21+
if (node.left != null && pathLengthMap.get(node.left) == null) {
22+
stack.add(node.left);
23+
} else if (node.right != null && pathLengthMap.get(node.right) == null) {
24+
stack.add(node.right);
25+
} else {
26+
stack.remove(stack.size() - 1);
27+
int leftPathLength = pathLengthMap.containsKey(node.left) ? pathLengthMap.get(node.left) : 0,
28+
rightPathLength = pathLengthMap.containsKey(node.right) ? pathLengthMap.get(node.right) : 0;
29+
diameter = Math.max(diameter, leftPathLength + rightPathLength);
30+
pathLengthMap.put(node, Math.max(leftPathLength, rightPathLength) + 1);
31+
}
32+
}
33+
34+
return diameter;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
*/
12+
var diameterOfBinaryTree = function(root) {
13+
let diameter = 0,
14+
pathLengthMap = new Map(),
15+
stack = []
16+
17+
if (root) stack.push(root)
18+
19+
while (stack.length > 0) {
20+
let node = stack[stack.length - 1]
21+
22+
if (node.left && pathLengthMap.get(node.left) === undefined) {
23+
stack.push(node.left)
24+
} else if (node.right && pathLengthMap.get(node.right) === undefined) {
25+
stack.push(node.right)
26+
} else {
27+
stack.pop()
28+
let leftPathLength = pathLengthMap.get(node.left) || 0,
29+
rightPathLength = pathLengthMap.get(node.right) || 0
30+
31+
diameter = Math.max(diameter, leftPathLength + rightPathLength)
32+
pathLengthMap.set(node, Math.max(leftPathLength, rightPathLength) + 1)
33+
}
34+
}
35+
36+
return diameter
37+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def diameterOfBinaryTree(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
diameter = 0
15+
path_length_map = {}
16+
stack = []
17+
18+
if root: stack.append(root)
19+
20+
while len(stack) > 0:
21+
node = stack[-1]
22+
if node.left and path_length_map.get(node.left) == None:
23+
stack.append(node.left)
24+
elif node.right and path_length_map.get(node.right) == None:
25+
stack.append(node.right)
26+
else:
27+
stack.pop()
28+
left_path_length = path_length_map.get(node.left, 0)
29+
right_path_length = path_length_map.get(node.right, 0)
30+
diameter = max(diameter, left_path_length + right_path_length)
31+
path_length_map[node] = max(left_path_length, right_path_length) + 1
32+
33+
return diameter
34+

0 commit comments

Comments
 (0)