算法笔记(二叉树1)

leetcode144 二叉树的前序遍历

递归版本

java 复制代码
public List<Integer> preorderTraversal(TreeNode root) {
    List<Integer> res = new ArrayList<>();
    preorder(root, res);
    return res;
}

public void preorder(TreeNode root, List<Integer> res) {
    if (root == null) {
        return;
    }
    res.add(root.val);
    preorder(root.left, res);
    preorder(root.right, res);
}

非递归版本:利用栈模拟

java 复制代码
public List<Integer> preorderTraversal(TreeNode root) {
    List<Integer> res = new ArrayList<>();
    //利用栈模拟非递归遍历
    Stack<TreeNode> stack = new Stack<>();
    if (root == null) {
        return res;
    }
    stack.push(root);
    while (!stack.isEmpty()) {
        TreeNode pop = stack.pop();
        res.add(pop.val);
        if (pop.right != null) {
            stack.push(pop.right);
        }
        if (pop.left != null) {
            stack.push(pop.left);
        }
    }
    return res;
}

leetcode94 二叉树的中序遍历

java 复制代码
 List<Integer> list = new ArrayList<>();

public List<Integer> inorderTraversal(TreeNode root) {
    mid(root);
    return list;
}

private void mid(TreeNode root) {
    if (root == null) return;
    mid(root.left);
    list.add(root.val);
    mid(root.right);
}

非递归版本

java 复制代码
public List<Integer> inorderTraversal(TreeNode root) {
    List<Integer> result = new ArrayList<>();
    if (root == null) {
        return result;
    }
    Stack<TreeNode> stack = new Stack<>();
    TreeNode cur = root;
    while (cur != null || !stack.isEmpty()) {
        if (cur != null) {
            stack.push(cur);
            cur = cur.left;
        } else {
            cur = stack.pop();
            result.add(cur.val);
            cur = cur.right;
        }
    }
    return result;
}

leetcode145 二叉树的后序遍历

java 复制代码
public List<Integer> postorderTraversal(TreeNode root) {
    List<Integer> list = new ArrayList();
    if (root == null) {
        return list;
    }
    postorder(root, list);
    return list;
}

private void postorder(TreeNode root, List<Integer> list) {
    if (root == null) {
        return;
    }
    postorder(root.left, list);
    postorder(root.right, list);
    list.add(root.val);
}

非递归版本

java 复制代码
public List<Integer> postorderTraversal(TreeNode root) {
    List<Integer> res = new ArrayList<>();
    if (root == null) {
        return res;
    }
    Stack<TreeNode> stack = new Stack<>();
    stack.push(root);
    while (!stack.isEmpty()) {
        TreeNode node = stack.pop();
        res.add(node.val);
        if (node.left != null) {
            stack.push(node.left);
        }
        if (node.right != null) {
            stack.push(node.right);
        }
    }
    Collections.reverse(res);
    return res;
}

leetcode102 二叉树的层序遍历

java 复制代码
public List<List<Integer>> levelOrder(TreeNode root) {
    List<List<Integer>> res = new ArrayList<>();
    Queue<TreeNode> queue = new LinkedList<>();
    if (root == null) {
        return res;
    }
    queue.add(root);
    while (!queue.isEmpty()) {
        int size = queue.size();
        List<Integer> temp = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            TreeNode node = queue.poll();
            temp.add(node.val);
            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
        }
        res.add(temp);
    }
    return res;
}

leetcode199 二叉树的右视图

在层序遍历中只加入该层的最后一个节点

java 复制代码
public List<Integer> rightSideView(TreeNode root) {
    List<Integer> res = new ArrayList<>();
    Queue<TreeNode> queue = new LinkedList<>();
    if (root == null) {
        return res;
    }
    queue.add(root);
    while (!queue.isEmpty()) {
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            TreeNode poll = queue.poll();
            if (poll.left != null) {
                queue.add(poll.left);
            }
            if (poll.right != null) {
                queue.add(poll.right);
            }
            if (i == size - 1) {
                res.add(poll.val);
            }
        }
    }
    return res;
}

leetcode637 二叉树层的平均值

java 复制代码
public List<Double> averageOfLevels(TreeNode root) {
    List<Double> res = new ArrayList<>();
    Queue<TreeNode> queue = new LinkedList<>();
    if (root == null) {
        return res;
    }
    queue.add(root);
    while (!queue.isEmpty()) {
        double temp = 0;
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            TreeNode node = queue.poll();
            temp += node.val;
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
        temp = temp / size;
        res.add(temp);
    }
    return res;
}

leetcode429 N叉树的层序遍历

java 复制代码
public List<List<Integer>> levelOrder(Node root) {
    List<List<Integer>> res = new ArrayList<>();
    Queue<Node> queue = new LinkedList<>();
    if (root == null) {
        return res;
    }
    queue.add(root);
    while (!queue.isEmpty()) {
        int size = queue.size();
        List<Integer> temp = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            Node poll = queue.poll();
            temp.add(poll.val);
            List<Node> children = poll.children;
            for (int j = 0; j < children.size(); j++) {
                if (children.get(j) != null) {
                    queue.add(children.get(j));
                }
            }
        }
        res.add(temp);
    }
    return res;
}

leetcode515 在每个树行中的最大值

java 复制代码
public List<Integer> largestValues(TreeNode root) {
    List<Integer> res = new ArrayList<>();
    if (root == null) {
        return res;
    }
    Queue<TreeNode> queue = new LinkedList<>();
    queue.add(root);
    while (!queue.isEmpty()) {
        int size = queue.size();
        int temp = Integer.MIN_VALUE;
        for (int i = 0; i < size; i++) {
            TreeNode poll = queue.poll();
            temp = Math.max(temp, poll.val);
            if (poll.left != null) {
                queue.add(poll.left);
            }
            if (poll.right != null) {
                queue.add(poll.right);
            }
        }
        res.add(temp);
    }
    return res;
}

leetcode116 填充每个节点的右侧指针

java 复制代码
public Node connect(Node root) {
    Queue<Node> queue = new LinkedList<>();
    if (root == null) {
        return null;
    }
    queue.add(root);
    while (!queue.isEmpty()) {
        int size = queue.size();
        Node poll = queue.poll();
        if (poll.left != null) {
            queue.add(poll.left);
        }
        if (poll.right != null) {
            queue.add(poll.right);
        }
        for (int i = 1; i < size; i++) {
            Node cur = queue.poll();
            if (cur.left != null) {
                queue.add(cur.left);
            }
            if (cur.right != null) {
                queue.add(cur.right);
            }
            poll.next = cur;
            poll = cur;
        }
    }
    return root;
}

leetcode104 二叉树的最大深度

java 复制代码
 public int maxDepth(TreeNode root) {
    if (root == null) {
        return 0;
    }
    int left = maxDepth(root.left);
    int right = maxDepth(root.right);
    return Math.max(left, right) + 1;
}

leetcode226 翻转二叉树

java 复制代码
public TreeNode invertTree(TreeNode root) {
    if (root == null) {
        return root;
    }
    TreeNode t = root.left;
    root.left = root.right;
    root.right = t;
    invertTree(root.left);
    invertTree(root.right);
    return root;
}

leetcode101 对称二叉树

java 复制代码
public boolean isSymmetric(TreeNode root) {
    if (root == null) {
        return true;
    }
    return check(root.left, root.right);
}

public boolean check(TreeNode left, TreeNode right) {
    if (left == null && right != null) {
        return false;
    } else if (left != null && right == null) {
        return false;
    } else if (left == null && right == null) {
        return true;
    } else if (left.val != right.val) {
        return false;
    }

    //比较外侧节点
    boolean bool1 = check(left.left, right.right);
    //比较内侧节点
    boolean bool2 = check(left.right, right.left);
    return bool1 && bool2;
}
相关推荐
shymoy41 分钟前
Radix Sorts
数据结构·算法·排序算法
木向2 小时前
leetcode92:反转链表||
数据结构·c++·算法·leetcode·链表
阿阿越2 小时前
算法每日练 -- 双指针篇(持续更新中)
数据结构·c++·算法
小爬虫程序猿5 小时前
如何利用Python解析API返回的数据结构?
数据结构·数据库·python
pianmian19 小时前
python数据结构基础(7)
数据结构·算法
ChoSeitaku12 小时前
链表交集相关算法题|AB链表公共元素生成链表C|AB链表交集存放于A|连续子序列|相交链表求交点位置(C)
数据结构·考研·链表
偷心编程12 小时前
双向链表专题
数据结构
香菜大丸12 小时前
链表的归并排序
数据结构·算法·链表
jrrz082812 小时前
LeetCode 热题100(七)【链表】(1)
数据结构·c++·算法·leetcode·链表
@小博的博客12 小时前
C++初阶学习第十弹——深入讲解vector的迭代器失效
数据结构·c++·学习