Leetcode - Populating Next Right Pointers in Each Node II

My code:

/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void connect(TreeLinkNode root) {
        if (root == null)
            return;
        
        Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
        q.offer(root);
        while (!q.isEmpty()) {
            int levelSize = q.size();
            TreeLinkNode next = null;
            for (int i = 0; i < levelSize; i++) {
                TreeLinkNode temp = q.poll();
                if (i == 0) {
                    temp.next = null;
                    next = temp;
                }
                else {
                    temp.next = next;
                    next = temp;
                }
                if (temp.right != null)
                    q.offer(temp.right);
                if (temp.left != null)
                    q.offer(temp.left);
            }
        }
        
    }
}

My test result:


Paste_Image.png

直接拿上面那道題目的代碼,就直接過測試了木西。乐埠。。

**
總結(jié): 右層序遍歷樹
**

Anyway, Good luck, Richardo!

My code:

/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void connect(TreeLinkNode root) {
        if (root == null)
            return;
        Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            TreeLinkNode prev = null;
            for (int i = 0; i < size; i++) {
                TreeLinkNode curr = q.poll();
                if (curr.right != null && curr.left != null) {
                    curr.right.next = prev;
                    curr.left.next = curr.right;
                    prev = curr.left;
                    q.offer(curr.right);
                    q.offer(curr.left);
                }
                else if (curr.right == null && curr.left == null)
                    continue;
                else if (curr.right != null) {
                    curr.right.next = prev;
                    prev = curr.right;
                    q.offer(curr.right);
                }
                else {
                    curr.left.next = prev;
                    prev = curr.left;
                    q.offer(curr.left);
                }
            }
        }
        
    }
}

這次我的代碼是過于復雜了點禽篱。

Anyway, Good luck, Richardo!

My code:

/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void connect(TreeLinkNode root) {
        if (root == null) {
            return;
        }
        
        Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            TreeLinkNode pre = null;
            for (int i = 0; i < size; i++) {
                TreeLinkNode curr = q.poll();
                curr.next = pre;
                pre = curr;
                if (curr.right != null) {
                    q.offer(curr.right);
                }
                if (curr.left != null) {
                    q.offer(curr.left);
                }
            }
        }
        
    }
}

以前的做法比較簡單畜伐,
但是
space: O(n)
time: O(n)

然后看到了
space 優(yōu)化到 O(1) 的做法,
自己寫了下:

My code:

/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void connect(TreeLinkNode root) {
        if (root == null) {
            return;
        }
        
        TreeLinkNode head = root;
        TreeLinkNode curr = null;
        TreeLinkNode prev = null;
        while (head != null) {
            curr = head;
            head = null;
            prev = null;
            while (curr != null) {
                if (curr.left != null) {
                    if (prev != null) {
                        prev.next = curr.left;
                    }
                    else {
                        head = curr.left;
                    }
                    prev = curr.left;
                }
                if (curr.right != null) {
                    if (prev != null) {
                        prev.next = curr.right;
                    }
                    else {
                        head = curr.right;
                    }
                    prev = curr.right;
                }
                curr = curr.next;
            }
        }
        
    }
}

reference:
https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution/7

其實也是一種BFS的思想

不得不說躺率,我的BFS理解真的很差玛界,解題能力也很差。
趕緊補悼吱。

Anyway, Good luck, Richardo! -- 09/06/2016

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末慎框,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子后添,更是在濱河造成了極大的恐慌笨枯,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件遇西,死亡現(xiàn)場離奇詭異猎醇,居然都是意外死亡,警方通過查閱死者的電腦和手機努溃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門硫嘶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人梧税,你說我怎么就攤上這事沦疾。” “怎么了第队?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵哮塞,是天一觀的道長。 經(jīng)常有香客問我凳谦,道長忆畅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任尸执,我火速辦了婚禮家凯,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘如失。我一直安慰自己绊诲,他們只是感情好,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布褪贵。 她就那樣靜靜地躺著掂之,像睡著了一般抗俄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上世舰,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天动雹,我揣著相機與錄音,去河邊找鬼跟压。 笑死胰蝠,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的裆馒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼丐怯,長吁一口氣:“原來是場噩夢啊……” “哼喷好!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起读跷,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤梗搅,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后效览,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體无切,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年丐枉,在試婚紗的時候發(fā)現(xiàn)自己被綠了哆键。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡瘦锹,死狀恐怖籍嘹,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情弯院,我是刑警寧澤辱士,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站听绳,受9級特大地震影響颂碘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜椅挣,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一头岔、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鼠证,春花似錦切油、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春攻谁,著一層夾襖步出監(jiān)牢的瞬間稚伍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工戚宦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留个曙,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓受楼,卻偏偏與公主長得像垦搬,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子艳汽,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容