美文网首页
LeetCode刷题TreeNode和ListNode -[ja

LeetCode刷题TreeNode和ListNode -[ja

作者: qiHuang112 | 来源:发表于2021-01-29 14:56 被阅读0次

1. ListNode

package basic;

public class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public ListNode(int v0, int... values) {
        this.val = v0;
        ListNode temp = this;
        for (int value : values) {
            temp.next = new ListNode(value);
            temp = temp.next;
        }
    }

    @Override
    public String toString() {
        return String.format("%d -> %s", this.val, this.next);
    }
}

2. TreeNode

package basic;

import java.util.LinkedList;

public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    public TreeNode(int v0, Integer... values) {
        this.val = v0;
        LinkedList<TreeNode> queue = new LinkedList<>();
        int index = 0;
        queue.addLast(this);
        while (index < values.length) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.removeFirst();
                if (index < values.length && values[index] != null) {
                    node.left = new TreeNode(values[index]);
                    queue.addLast(node.left);
                }
                index++;
                if (index < values.length && values[index] != null) {
                    node.right = new TreeNode(values[index]);
                    queue.addLast(node.right);
                }
                index++;
            }
        }
    }

    @Override
    public String toString() {
        LinkedList<Integer> arr = new LinkedList<>();
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.addLast(this);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.removeFirst();
                if (node != null) {
                    arr.addLast(node.val);
                    queue.addLast(node.left);
                    queue.addLast(node.right);
                } else {
                    arr.addLast(null);
                }
            }
        }
        while (arr.peekLast() == null) {
            arr.removeLast();
        }
        return arr.toString();
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1, null, 2, 3, 4, 5);
        System.out.println(root);
    }
}

相关文章

网友评论

      本文标题:LeetCode刷题TreeNode和ListNode -[ja

      本文链接:https://www.haomeiwen.com/subject/bibqtltx.html