-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.kt
34 lines (29 loc) · 924 Bytes
/
Solution.kt
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 _543.kotlin
import structure.TreeNode
/**
* @author relish
* @since 2018/05/10
*
* Definition for a binary tree node.
* class TreeNode(var `val`: Int = 0) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun diameterOfBinaryTree(root: TreeNode?): Int {
if (root == null || root.left == null && root.right == null) return 0
val height = depth(root.left) + depth(root.right)
return maxOf(height, maxOf(diameterOfBinaryTree(root.left), diameterOfBinaryTree(root.right)))
}
fun depth(root: TreeNode?): Int {
if (root == null) return 0
return 1 + maxOf(depth(root.left), depth(root.right))
}
}
fun main(args: Array<String>) {
// val root = TreeNode.createTestData("[1,2]")
val root = TreeNode.createTestData("[1,2,3,4,5]")
TreeNode.print(root)
println(Solution().diameterOfBinaryTree(root))
}