-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem543.java
More file actions
34 lines (30 loc) · 809 Bytes
/
Problem543.java
File metadata and controls
34 lines (30 loc) · 809 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.leetcode.problems;
class Solution543 {
public int diameterOfBinaryTree(TreeNode root) {
if(root==null)
{
return 0;
}
int left = treeLength(root.left);
int right = treeLength(root.right);
int res = left+right;
int l = diameterOfBinaryTree(root.left);
int r = diameterOfBinaryTree(root.right);
return Math.max(l, Math.max(r, res));
}
public int treeLength(TreeNode root)
{
if(root==null)
{
return 0;
}
int left = treeLength(root.left);
int right = treeLength(root.right);
return 1+Math.max(left, right);
}
}
public class Problem543 {
public static void main(String[] args) {
System.out.println("hello, world");
}
}