-
Notifications
You must be signed in to change notification settings - Fork 5
/
SmallestSubtreeWithAllTheDeepestNodes.java
155 lines (147 loc) · 3.92 KB
/
SmallestSubtreeWithAllTheDeepestNodes.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
TreeNode result;
int max;
public TreeNode subtreeWithAllDeepest(TreeNode root) {
result = null;
max = getHeight(root);
solve(root,0);
return result;
}
private int solve(TreeNode root, int val)
{
if (root == null) return val;
int left = solve(root.left,val+1);
int right = solve(root.right,val+1);
if (left == max && right == max)
result = root;
return Math.max(left,right);
}
private int getHeight(TreeNode root)
{
if (root == null) return 0;
return Math.max(getHeight(root.left),getHeight(root.right))+1;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
TreeNode n1, n2;
int max;
public TreeNode subtreeWithAllDeepest(TreeNode root) {
n1 = n2 = null;
max = -1;
getTargets(root,0);
return solve(root,n1.val,n2.val);
}
private void getTargets(TreeNode root, int level)
{
if (root == null) return;
getTargets(root.left,level+1);
getTargets(root.right,level+1);
if (level > max)
{
max = level;
n1 = n2 = root;
}
else if (level == max)
n2 = root;
}
private TreeNode solve(TreeNode root, int a, int b) {
if (root == null) return null;
if (root.val == a || root.val == b) return root;
TreeNode left = solve(root.left, a, b);
TreeNode right = solve(root.right, a, b);
if (left != null && right != null) return root;
if (left != null) return left;
if (right != null) return right;
return null;
}
}
class Solution {
public TreeNode subtreeWithAllDeepest(TreeNode root) {
if(root==null)
return root;
int leftDepth = getDepth(root.left);
int rightDepth = getDepth(root.right);
if(leftDepth==rightDepth)
return root;
else if(leftDepth> rightDepth)
return subtreeWithAllDeepest(root.left);
else
return subtreeWithAllDeepest(root.right);
}
public int getDepth(TreeNode root)
{
if(root==null)
return 0;
return 1 + Math.max(getDepth(root.left), getDepth(root.right));
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Pair
{
TreeNode a;
int b;
Pair(TreeNode a, int b)
{
this.a = a;
this.b = b;
}
}
class Solution {
public TreeNode subtreeWithAllDeepest(TreeNode root) {
return solve(root).a;
}
private Pair solve(TreeNode root)
{
if (root == null) return new Pair(null,0);
Pair left = solve(root.left);
Pair right = solve(root.right);
if (left.b == right.b) return new Pair(root,left.b+1);
if (left.b > right.b) return new Pair(left.a,left.b+1);
return new Pair(right.a,right.b+1);
}
}