use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _199 method rightSideView.
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode curr = q.poll();
if (i == size - 1) {
result.add(curr.val);
}
if (curr.left != null) {
q.offer(curr.left);
}
if (curr.right != null) {
q.offer(curr.right);
}
}
}
return result;
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _623 method dfs.
private void dfs(TreeNode root, int v, int d) {
if (root == null) {
return;
}
if (d == 2) {
TreeNode newLeft = new TreeNode(v);
TreeNode newRight = new TreeNode(v);
newLeft.left = root.left;
newRight.right = root.right;
root.left = newLeft;
root.right = newRight;
} else {
dfs(root.left, v, d - 1);
dfs(root.right, v, d - 1);
}
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _637 method averageOfLevels.
public List<Double> averageOfLevels(TreeNode root) {
List<Double> result = new ArrayList<>();
if (root == null) {
return result;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
double sum = 0.0;
for (int i = 0; i < size; i++) {
TreeNode curr = queue.poll();
if (curr != null) {
sum += curr.val;
}
if (curr.left != null) {
queue.offer(curr.left);
}
if (curr.right != null) {
queue.offer(curr.right);
}
}
result.add(sum / size);
}
return result;
}
use of com.fishercoder.common.classes.TreeNode in project Leetcode by fishercoder1534.
the class _145Test method test1.
@Test
public void test1() {
root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
expected = new ArrayList<>(Arrays.asList(2, 3, 1));
assertEquals(expected, test.postorderTraversal_recursive(root));
}
Aggregations