《leetCode》:Populating Next Right Pointers in Each Node
来源:互联网 发布:sql 表 设计 编辑:程序博客网 时间:2024/05/29 17:42
题目
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.Initially, all next pointers are set to NULL.Note:You may only use constant extra space.You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).For example,Given the following perfect binary tree, 1 / \ 2 3 / \ / \ 4 5 6 7After calling your function, the tree should look like: 1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
思路
思路,还是一样借助两个队列来进行完成,只要是关于按层来处理二叉树,一般就是借助两个队列来完成。
实现代码如下:
/** * 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> q1=new LinkedList<TreeLinkNode>(); Queue<TreeLinkNode> q2=new LinkedList<TreeLinkNode>(); q1.add(root); root.next=null; while(!(q1.isEmpty()&&q2.isEmpty())){ while(!q1.isEmpty()){ TreeLinkNode node=q1.poll(); if(node.left!=null){ q2.add(node.left); } if(node.right!=null){ q2.add(node.right); } } while(!q2.isEmpty()){ TreeLinkNode node=q2.poll(); if(q2.isEmpty()){ node.next=null; } else{ node.next=q2.peek(); } q1.add(node); } } }}
一次就AC了,哈哈哈哈
1 0
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- leetcode Populating Next Right Pointers in Each Node &&Populating Next Right Pointers in Each Node||
- LeetCode Populating Next Right Pointers in Each Node & Populating Next Right Pointers in Each Node I
- 【Leetcode】Populating Next Right Pointers in Each Node in JAVA
- LeetCode Populating Next Right Pointers in Each Node
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node
- LeetCode: Populating Next Right Pointers in Each Node
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node
- [LeetCode] Populating Next Right Pointers in Each Node II
- 【leetcode】 Populating Next Right Pointers in Each Node
- leetcode 78: Populating Next Right Pointers in Each Node
- leetcode 79: Populating Next Right Pointers in Each Node II
- Leetcode: Populating Next Right Pointers in Each Node
- [Leetcode] Populating Next Right Pointers in Each Node ii
- leetcode Populating Next Right Pointers in Each Node II
- 【leetcode】Populating Next Right Pointers in Each Node
- 2012年及之后的ImageNet比赛的冠军、亚军和季军ImageNet winners after 2012
- poj1005--小学图形题
- 屏幕适配
- HDU 5627 Clarke and MST &高位枚举+并查集
- jQuery-源码阅读,pushStack()入栈方法
- 《leetCode》:Populating Next Right Pointers in Each Node
- 凸包学习笔记
- 正则表达式类判断字符串内是否有非数字的字符
- 4408: [Fj Winter Camp 2016]神秘数&&4299: Codechef FRBSUM|主席树
- 算法代码实现之冒泡排序,C/C++ 实现
- Json数据格式解析·编译器观点
- [NOIP 2011] Mayan游戏
- 操作系统
- Gym 100203I I WIN(最大流)