Populating Next Right Pointers in Each Node (LeetCode)
来源:互联网 发布:java微信二次开发视频 编辑:程序博客网 时间:2024/06/14 06:40
题目:
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 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
题目分析:
题目中给出一个完美二叉树。每个节点有left、right、next三个指针。left、right已经给好。要求将next指向自己右边的兄弟节点。如果已经是最右边,则指向null。
思路:
- 使用广度优先遍历二叉树
- 建立一个队列queue,每次将节点的左孩子、右孩子分别放入queue中。
- 下一个需要遍历的点,即为queue中的头结点。
- 用tag标记所在层的最右边的节点。
- 遍历到tag之前,将所在节点的next指向queue中的头结点
- 遍历到tag时,把next指向null,左孩子、右孩子入队列。将tag的右孩子标记为tag。
注意点:
- 要注意queue接口的用法。
- queue中,poll和remove的区别
代码:
/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; * TreeLinkNode(int x) { val = x; } * } */import java.util.*;public class Solution { public void connect(TreeLinkNode root) { if (root == null){ return; } Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>(); TreeLinkNode tag = root; TreeLinkNode thisNode = root; while (thisNode != null){ if (thisNode == tag){ if (thisNode.left != null){ queue.add(thisNode.left); queue.add(thisNode.right); tag = thisNode.right; } thisNode.next = null; thisNode = queue.poll(); }else{ thisNode.next = queue.poll(); if (thisNode.left != null){ queue.add(thisNode.left); queue.add(thisNode.right); } thisNode = thisNode.next; } } }}
0 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
- sqlite数据库操作
- SDUT 1500 Message Flood(字典树 && 模板)
- hdu 3345 War Chess (bfs+优先队列)
- python 备忘录
- 安卓学习谱
- Populating Next Right Pointers in Each Node (LeetCode)
- fatal error C1047 解决方法
- 回归树
- fatal error C1900
- 新手 对用hibernate+struts 分页的一些感悟
- matlab中图例换行
- suse 11安装db2时The 32-bit library file libpam.so is not found on the system.
- python dir()函数作用
- 关于HTML5中Canvas的宽、高设置有关问题