-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeDepth.java
More file actions
36 lines (28 loc) · 825 Bytes
/
Copy pathTreeDepth.java
File metadata and controls
36 lines (28 loc) · 825 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
35
36
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
// Note the follow up of this: "check if is valid binary search tree"
public class Solution {
// private int left = 0;
// private int right = 0;
public int TreeDepth(TreeNode root) {
if (root == null) return 0;
int l = getHeight(root.left, 0);
int r = getHeight(root.right, 0);
return (l > r) ? (l + 1) : (r + 1);
}
public int getHeight(TreeNode root, int cur) {
if (root == null) return 0;
int left = getHeight(root.left, cur);
int right = getHeight(root.right, cur);
int h = (left > right) ? (left + 1) : (right + 1);
return h;
}
}