iT邦幫忙

2025 iThome 鐵人賽

DAY 0
0
自我挑戰組

Leetcode30天挑戰系列 第 17

Day17-Populating Next Right Pointers in Each Node

  • 分享至 

  • xImage
  •  

今天的題目為116.Populating Next Right Pointers in Each Node,題目再叫我們將每個節點的next指標指向它右邊同一層的節點,如果該節點是該層的最右邊節點,則將 next設為 NULL,而且初始時,所有節點的 next 指標都被設為 NULL。

以下為程式碼:

class Solution {
    public Node connect(Node root) {
        if (root == null || root.left == null) return root;

        root.left.next = root.right;

        if (root.next != null) {
            root.right.next = root.next.left;
        }

        connect(root.left);
        connect(root.right);

        return root;
    }
}

今天的跟上一篇比比較簡單一點,今天的是以前有學過的類似內容,所以比較清楚!


上一篇
Day16-Distinct Subsequences
下一篇
Day18-Populating Next Right Pointers in Each Node II
系列文
Leetcode30天挑戰30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言