算法笔记(二叉树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;
}
相关推荐
代码雕刻家几秒前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
Kalika0-01 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
代码雕刻家2 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
小字节,大梦想3 小时前
【C++】二叉搜索树
数据结构·c++
我是哈哈hh3 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
丶Darling.4 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo5204 小时前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法
Indigo_code4 小时前
【数据结构】【链表代码】合并有序链表
数据结构·windows·链表
jiyisuifeng19914 小时前
代码随想录训练营第54天|单调栈+双指针
数据结构·算法
我言秋日胜春朝★4 小时前
【C++】红黑树
数据结构