7、二叉树

代码随想录
LeetCode 题解

1、二叉树的遍历

image

1.1、前序遍历

144. 二叉树的前序遍历

public List<Integer> preorderTraversal(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    preorder(root, list);
    return list;
}

private void preorder(TreeNode node, List<Integer> list) {
    if (node == null) return;

    list.add(node.val);
    preorder(node.left, list);
    preorder(node.right, list);
}
public List<Integer> preorderTraversal(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    if (root == null) return list;

    Deque<TreeNode> stack = new LinkedList<>();
    stack.push(root);
    while (!stack.isEmpty()) {
        TreeNode cur = stack.pop(); // 头 左 右

        list.add(cur.val);
        if (cur.right != null) stack.push(cur.right);
        if (cur.left != null) stack.push(cur.left);
    }

    return list;
}
private class Command {
    public String s; // go, print
    public TreeNode node;

    public Command(String s, TreeNode node) {
        this.s = s;
        this.node = node;
    }
}

public List<Integer> preorderTraversal(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    if (root == null) return list;

    Deque<Command> stack = new LinkedList<>();
    stack.push(new Command("go", root));
    while (!stack.isEmpty()) {
        Command command = stack.pop();
        String s = command.s;
        TreeNode node = command.node;

        if (s.equals("print")) list.add(node.val);
        else {
            if (node.right != null) stack.push(new Command("go", node.right));
            if (node.left != null) stack.push(new Command("go", node.left));
            stack.push(new Command("print", node));
        }
    }

    return list;
}

1.2、中序遍历

94. 二叉树的中序遍历

public List<Integer> inorderTraversal(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    inorder(root, list);
    return list;
}

private void inorder(TreeNode node, List<Integer> list) {
    if (node == null) return;

    inorder(node.left, list);
    list.add(node.val);
    inorder(node.right, list);
}
public List<Integer> inorderTraversal(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    if (root == null) return list;

    Deque<TreeNode> stack = new LinkedList<>();
    TreeNode cur = root;
    while (cur != null || !stack.isEmpty()) {
        if (cur != null) {
            stack.push(cur);
            cur = cur.left;
        } else {
            cur = stack.pop();
            list.add(cur.val);
            cur = cur.right;
        }
    }

    return list;
}
private class Command {
    public String s; // go, print
    public TreeNode node;

    public Command(String s, TreeNode node) {
        this.s = s;
        this.node = node;
    }
}

public List<Integer> inorderTraversal(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    if (root == null) return list;

    Deque<Command> stack = new LinkedList<>();
    stack.push(new Command("go", root));
    while (!stack.isEmpty()) {
        Command command = stack.pop();
        String s = command.s;
        TreeNode node = command.node;

        if (s.equals("print")) list.add(node.val);
        else {
            if (node.right != null) stack.push(new Command("go", node.right));
            stack.push(new Command("print", node));
            if (node.left != null) stack.push(new Command("go", node.left));
        }
    }

    return list;
}

1.3、后序遍历

145. 二叉树的后序遍历

public List<Integer> postorderTraversal(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    postorder(root, list);
    return list;
}

private void postorder(TreeNode node, List<Integer> list) {
    if (node == null) return;

    postorder(node.left, list);
    postorder(node.right, list);
    list.add(node.val);
}
public List<Integer> postorderTraversal(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    if (root == null) return list;

    Deque<TreeNode> stack1 = new LinkedList<>();
    Deque<TreeNode> stack2 = new LinkedList<>();
    stack1.push(root);
    while (!stack1.isEmpty()) {
        TreeNode cur = stack1.pop(); // 头 右 左

        stack2.push(cur);
        if (cur.left != null) stack1.push(cur.left);
        if (cur.right != null) stack1.push(cur.right);
    }
    while (!stack2.isEmpty()) list.add(stack2.pop().val);

    return list;
}
private class Command {
    public String s; // go, print
    public TreeNode node;

    public Command(String s, TreeNode node) {
        this.s = s;
        this.node = node;
    }
}

public List<Integer> postorderTraversal(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    if (root == null) return list;

    Deque<Command> stack = new LinkedList<>();
    stack.push(new Command("go", root));
    while (!stack.isEmpty()) {
        Command command = stack.pop();
        String s = command.s;
        TreeNode node = command.node;

        if (s.equals("print")) list.add(node.val);
        else {
            stack.push(new Command("print", node));
            if (node.right != null) stack.push(new Command("go", node.right));
            if (node.left != null) stack.push(new Command("go", node.left));
        }
    }

    return list;
}

1.4、层序遍历

102. 二叉树的层序遍历

public List<List<Integer>> levelOrder(TreeNode root) {
    List<List<Integer>> list = new ArrayList<>();
    if (root == null) return list;

    Queue<TreeNode> queue = new LinkedList<>();
    queue.add(root);
    while (!queue.isEmpty()) {
        int size = queue.size();
        List<Integer> level = new ArrayList<>();

        for (int i = 0; i < size; i++) {
            TreeNode cur = queue.remove();
            level.add(cur.val);
            if (cur.left != null) queue.add(cur.left);
            if (cur.right != null) queue.add(cur.right);
        }

        list.add(level);
    }

    return list;
}

2、二叉树的属性

3、二叉树的修改与构造

4、二叉搜索树的属性

5、二叉树公共祖先

6、二叉树搜索的修改与构造

posted @ 2023-11-20 23:04  lidongdongdong~  阅读(10)  评论(0编辑  收藏  举报