LeetCode 116. Populating Next Right Pointers in Each Node — JavaScript

土耳其有直飛…,要不要飛去晃一下…
var connect = function(root) {
let temp = root;
while (temp && temp.left) {
const next = temp.left;
while (temp) {
temp.left.next = temp.right;
temp.right.next = temp.next && temp.next.left;
temp = temp.next
}
temp = next;
}
return root;
};

一層一層遍歷樹即可,關鍵在於此題是完美二元樹,不用考慮缺節點的情形

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
黃韋智

自學寫程式,目前爲 React 前端工程師。熱愛旅遊,將近 30 個國家,足跡遍佈亞洲與歐洲。生命與街舞已經離不開,歡迎訂閱 Youtube 頻道:https://www.youtube.com/channel/UCEU-bEDl7R-iGyLVZFae33g