成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

117. Populating Next Right Pointers In Each NodeII

jackwang / 623人閱讀

題目:
Follow up for problem "Populating Next Right Pointers in Each Node".

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

You may only use constant extra space.
For example,
Given the following binary tree,

     1
   /  
  2    3
 /     
4   5    7

After calling your function, the tree should look like:

     1 -> NULL
   /  
  2 -> 3 -> NULL
 /     
4-> 5 -> 7 -> NULL

解答:

public class Solution {
    public void connect(TreeLinkNode root) {
        //重點(diǎn)是記錄current, head, prev這三個結(jié)點(diǎn)
        TreeLinkNode curt = root;
        TreeLinkNode head = null;
        TreeLinkNode prev = null;
        
        while (curt != null) {
            while (curt != null) {
                if (curt.left != null) {
                    if (prev != null) {
                        prev.next = curt.left;
                    } else {
                        head = curt.left;
                    }
                    prev = curt.left;
                }
                if (curt.right != null) {
                    if (prev != null) {
                        prev.next = curt.right;
                    } else {
                        head = curt.right;
                    }
                    prev = curt.right;
                }
                curt = curt.next;
            }
            curt = head;
            head = null;
            prev = null;
        }
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/64883.html

相關(guān)文章

  • leetcode116-117. Populating Next Right Pointers in

    摘要:題目要求給一個完全二叉樹,將當(dāng)前節(jié)點(diǎn)的值指向其右邊的節(jié)點(diǎn)。而在中則是提供了一個不完全的二叉樹,其它需求沒有發(fā)生改變。我們需要使用一個變量來存儲父節(jié)點(diǎn),再使用一個變量來存儲當(dāng)前操作行的,將前一個指針指向當(dāng)前父節(jié)點(diǎn)的子節(jié)點(diǎn)。 題目要求 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; ...

    coolpail 評論0 收藏0
  • [Leetcode] Populating Next Right Pointers in Each

    摘要:原題鏈接廣度優(yōu)先搜索復(fù)雜度時間空間思路相當(dāng)于是遍歷二叉樹。代碼記錄本層節(jié)點(diǎn)的個數(shù)最后一個節(jié)點(diǎn)的是,不做處理遞歸法復(fù)雜度時間空間遞歸??臻g思路由于父節(jié)點(diǎn)多了指針,我們不用記錄父節(jié)點(diǎn)的父節(jié)點(diǎn)就能直到它相鄰的節(jié)點(diǎn)。 Populating Next Right Pointers in Each Node I Given a binary tree struct TreeLinkNode { ...

    miracledan 評論0 收藏0
  • LeetCode[117] Population Next Right Pointers in Ea

    摘要:復(fù)雜度思路設(shè)置一個指向下一層要遍歷的節(jié)點(diǎn)的開頭的位置。 LeetCode[117] Population Next Right Pointers in Each Node II 1 / 2 3 / 4 5 7 After calling your function, the tree should look like: ...

    lijinke666 評論0 收藏0
  • [LeetCode] 426. Convert BST to Sorted Doubly Linke

    Problem Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Lets take the foll...

    MartinDai 評論0 收藏0
  • Life of a Pixel 2018

    摘要:將操作記錄為一列。在列表上進(jìn)行操作被稱為光柵化。能夠運(yùn)行產(chǎn)生位圖的命令加速光柵化注意到此時像素并不在屏幕上。因此,線程將劃分為。根據(jù)與的距離確定的優(yōu)先級。被包裝在一個中,該對象被提交給瀏覽器進(jìn)程。 This talk is about how Chrome turns web content into pixels. The entire process is called rend...

    Miracle 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<