LeetCode#117. Populating Next Right Pointers in Each Node II
- 題目:將一個二叉樹按照規則進行連線。規則為:將節點的下一個指標指向下一個節點 (不能使用額外的儲存空間)
- 難度:Medium
- 思路:由於題目要求中的二叉樹不再是完全二叉樹,因此116題的方法不能直接套用。於是在不考慮額外空間的條件下,給出一個非最優解:利用佇列來實現
- 程式碼:
不考慮“不能使用額外儲存空間”
public class Solution {
public void connect(TreeLinkNode root) {
if(root == null){
return ;
}
LinkedList<TreeLinkNode> queue = new LinkedList<>();
queue.offer(root);
while(queue.size() > 0){
int size = queue.size();
TreeLinkNode pre = queue.poll();
TreeLinkNode curr = null;
if(pre.left != null){
queue.offerLast(pre.left);
}
if (pre.right != null){
queue.offerLast(pre.right);
}
for(int i = 1; i < size; i++){
curr = queue.poll();
pre.next = curr;
pre = curr;
if(pre.left != null){
queue.offerLast(pre.left );
}
if(pre.right != null){
queue.offerLast(pre.right);
}
}
pre.next = null;
}
}
}
不使用額外儲存空間
這裡寫程式碼片
相關推薦
leetcode: 117. Populating Next Right Pointers in Each Node II
Difficulty Medium. Problem Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }
[LeetCode] 117. Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeL
Leetcode 117. Populating Next Right Pointers in Each Node II
文章作者:Tyan 部落格:noahsnail.com | CSDN | 簡書 1. Description 2. Solution Version 1 /** * Definition for bin
LeetCode 117.Populating Next Right Pointers in Each Node II (填充同一層的兄弟節點 II)
題目描述: 給定一個二叉樹 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } 填充它的每個 next 指標,讓這個指標指向其下一個右側節
LeetCode#117. Populating Next Right Pointers in Each Node II
題目:將一個二叉樹按照規則進行連線。規則為:將節點的下一個指標指向下一個節點 (不能使用額外的儲存空間) 難度:Medium 思路:由於題目要求中的二叉樹不再是完全二叉樹,因此116題的方法不能直接套
LeetCode-117-Populating Next Right Pointers in Each Node II
clas fun The ant fine ppr pro like problem 算法描述: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right;
117. Populating Next Right Pointers in Each Node II
point tar 規則 owin oot hat || div strong Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree coul
117. Populating Next Right Pointers in Each Node II - Medium
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to it
python leetcode 116. Populating Next Right Pointers in Each Node 117 II
不難,考察程式碼編寫能力 116. Populating Next Right Pointers in Each Node class Solution: # @param root, a tree link node # @return nothing de
[和小菜雞一起刷題(python)] LeetCode 117 填充同一層的兄弟節點 II (Populating Next Right Pointers in Each Node II)
LeetCode 117. 填充同一層的兄弟節點 II(Populating Next Right Pointers in Each Node II) 原題 思路 程式碼 原題 給定一個二叉樹 struct TreeLinkNode
【leetcode】117.(Medium)Populating Next Right Pointers in Each Node II
解題思路: 使用兩個佇列輪番記錄每排結點的情況 提交程式碼: class Solution { public void connect(TreeLinkNode root) { if(root==null) return; Queue&l
leetcode 117. 填充每個節點的下一個右側節點指針 II(Populating Next Right Pointers in Each Node II)
常量 設置 public ref etc 圖片 lee nec 一個 目錄 題目描述: 示例: 解法: 題目描述: 給定一個二叉樹 struct Node {
LeetCode 116 Populating Next Right Pointers in Each Node
next following each || ble tac () pub void Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; Tree
leetcode: 116. Populating Next Right Pointers in Each Node
Difficulty Medium. Problem Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }
Leetcode 116. Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next point
[LeetCode] 116. Populating Next Right Pointers in Each Node
題目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each n
LeetCode 116.Populating Next Right Pointers in Each Node (填充同一層的兄弟節點)
題目描述: 給定一個二叉樹 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } 填充它的每個 next 指標,讓這個指標指向其下一個右側節點
[LeetCode] Populating Next Right Pointers in Each Node II 每個節點的右向指標之二
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
leetcode 之Populating Next Right Pointers in Each Node解題思路
題目如下: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;
LeetCode(49) Populating Next Right Pointers in Each Node I II
Populating Next Right Pointers in Each Node I 題目描述 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right