Search in sources :

Example 36 with TreeNode

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;
}
Also used : TreeNode(com.fishercoder.common.classes.TreeNode) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 37 with TreeNode

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);
    }
}
Also used : TreeNode(com.fishercoder.common.classes.TreeNode)

Example 38 with TreeNode

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;
}
Also used : TreeNode(com.fishercoder.common.classes.TreeNode) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 39 with TreeNode

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));
}
Also used : TreeNode(com.fishercoder.common.classes.TreeNode) Test(org.junit.Test)

Aggregations

TreeNode (com.fishercoder.common.classes.TreeNode)39 Test (org.junit.Test)24 ArrayList (java.util.ArrayList)7 LinkedList (java.util.LinkedList)6 HashMap (java.util.HashMap)2 List (java.util.List)2 Before (org.junit.Before)2 ArrayDeque (java.util.ArrayDeque)1 TreeMap (java.util.TreeMap)1 Ignore (org.junit.Ignore)1