LeetCode-116. 填充每个节点的下一个右侧节点指针-Java-medium

题目链接

Node
public class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;
    public Node() {};
    public Node(int val) {this.val = val;}
    public Node(int val, Node left, Node right, Node next){
        this.val = val;
        this.left = left;
        this.right = right;
        this.next = next;
    }
}
NextTreeOperation
public class NextTreeOperation {
    /**
     * 层序遍历构建带next指针的二叉树
     *
     * @param arr
     * @return root
     */
    public Node createNextTreeByLayerOrder(List<Integer> arr) {
        if (arr.size() == 0) {
            return new Node();
        }
        int index = 0;
        Queue<Node> q = new LinkedList<>();
        Node root = new Node(arr.get(index++));
        q.offer(root);
        while (!q.isEmpty() && index < arr.size()) {
            Node now = q.poll();
            if (arr.get(index) != null) {
                now.left = new Node(arr.get(index));
                q.offer(now.left);
            }
            index++;
            if (index >= arr.size()) { // 防止数组越界
                break;
            }
            if (arr.get(index) != null) {
                now.right = new Node(arr.get(index));
                q.offer(now.right);
            }
            index++;
        }
        return root;
    }

    /**
     * 层序遍历带next指针的二叉树
     *
     * @param root
     */
    public void levelTraversal(Node root) {
        Queue<Node> q = new LinkedList<>();
        q.offer(root);
        while (!q.isEmpty() && q.peek() != null) {
            for (int i = q.size(); i > 0; i--) { // q.size()会变
                Node now = q.poll();
                System.out.print(now.val + " ");
                if (now.left != null) {
                    q.offer(now.left);
                }
                if (now.right != null) {
                    q.offer(now.right);
                }
            }
            System.out.println();
        }
        System.out.println();
    }
    
    /**
     * 通过next遍历带next指针的二叉树
     *
     * @param root
     */
    public void levelTraversalByNext(Node root) {
        while (root != null) {
            Node now = root;
            while (now != null) {
                System.out.print(now.val + " ");
                now = now.next;
            }
            System.out.println("# ");
            root = root.left;
        }
    }
}
法一
    /**
     * 在前序遍历过程中填充next指针
     *
     * @param root
     * @return
     */
    public Node connect(Node root) {
        if (root == null || root.left == null) {
            return root;
        }
        root.left.next = root.right; // root的左孩子连接到root的右孩子
        if (root.next != null) {
            root.right.next = root.next.left; // root的右孩子连接到root.next的左孩子
        }
        connect(root.left);
        connect(root.right);
        return root;
    }
本地测试
        /**
         * 116. 填充每个节点的下一个右侧节点指针
         */
        lay.showTitle(116);
        Solution116 sol116 = new Solution116();
        List<Integer> arr116 = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
        Node root116 = nextTreeOpt.createNextTreeByLayerOrder(arr116);
        nextTreeOpt.levelTraversal(root116);
        sol116.connect(root116);
        nextTreeOpt.levelTraversalByNext(root116);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值