这题没啥难度,主要是仔细了
/** * 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; } int k = 0; int length = (int)Math.pow(2, k); LinkedList<TreeLinkNode> queue = new LinkedList<TreeLinkNode>(); queue.add(root); TreeLinkNode preNode = null; while (!queue.isEmpty()){ TreeLinkNode node = queue.remove(); if(node.left != null){ queue.add(node.left); } if(node.right!=null){ queue.add(node.right); } if(preNode != null){ preNode.next=node; } if(length == 1){ length = (int)Math.pow(2, ++k); node.next=null; preNode = null; }else{ preNode = node; length--; } } } }
相关推荐
java java_leetcode题解之Populating Next Right Pointers in Each Node.java
java基础 java_leetcode题解之Populating Next Right Pointers in Each Node
javascript js_leetcode题解之116-populating-next-right-pointers-in-each-node.js
python python_leetcode题解116_Populating_Next_Right_Pointers_in_Each_Node
javascript js_leetcode题解之117-populating-next-right-pointers
public void helper(TreeNode root, int level){// 当前层没有 list,新建// 取得当前层的 list迭代pub
python python_leetcode题解之117_Populating_Next_Right_Pointers
python python_leetcode题解之237_Delete_Node_in_a_Linked_List.py
LeetCode 按层次遍历二叉树 按层次遍历二叉树 按层次遍历二叉树 按层次遍历二叉树 按层次遍历二叉树
c c语言_leetcode 0019_remove_nth_node_from_end_of_list.zip
LeetCode题解 - Java语言实现 LeetCode是一个非常流行的编程挑战平台,为程序员和开发者提供了大量的编程挑战题目,以帮助他们提高编程技能和解决问题的能力。本文档为LeetCode题解的Java语言实现,涵盖了181页的...
[117]填充每个节点的下一个右侧节点指针 II|populating-next-right-pointers-in-each-node-ii给定一个二叉树填充
leetcode卡 leetcode_python 项目介绍 想学学python,刷刷leetcode 打卡轨迹 2020-01-13 70 爬楼梯 2020-01-14 120 Triangle 2020-01-15 213 House Robberll -变种 198 337 2020-01-16 139 单词拆分 2020-01-20 104 ...
c语言入门 c语言_leetcode题解496-next-greater-element-i.c
js js_leetcode题解之19-remove-nth-node-from-end-of-list.js
c语言入门 C语言_leetcode题解之19-remove-nth-node-from-end-of-list.c
java java_leetcode-73-set-matrix-zeroes
java java_leetcode-110-balanced-binary-tree