leetcode--Populating Next Right Pointers in Each Node
来源:互联网 发布:linux如何强制退出 编辑:程序博客网 时间:2024/06/09 21:57
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 toNULL
.
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
题意:对于一个完美二叉树,将所有节点的next指向其同层右边的节点,每一层的最后一个节点,指向null
分类:二叉树
解法1:层次遍历,使用两个栈,stack1存储当前层,stack2存储下一层,对于stack1,每次pop(),将左右子树添加到stack2,同时pre指向前一个节点,连接pre和当前元素,直到stack1为空,此时stack2装满下一层元素,将stack2元素复制到stack1,清空stack2,重复上述过程。
本质就是层次遍历,特点是使用了两个栈。
/** * 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;Stack<TreeLinkNode> stack1 = new Stack<TreeLinkNode>();List<TreeLinkNode> stack2 = new ArrayList<TreeLinkNode>();stack1.add(root);while(stack1.size()>0||stack2.size()>0){TreeLinkNode pre = null;while(stack1.size()>0){TreeLinkNode t = stack1.pop();if(t.left!=null) stack2.add(t.left);if(t.right!=null) stack2.add(t.right);if(pre!=null){pre.next = t;}pre = t;}Collections.reverse(stack2);stack1.addAll(stack2);stack2.clear();} }}
解法2:递归解决。创建一个能够连接相邻节点,以及这个两个节点的所有子节点的函数。
/** * 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; helper(root,null); } /** * 该函数用于连接root的子节点,next的子节点 * root为当前节点,next为其右边的节点 */ public void helper(TreeLinkNode root,TreeLinkNode next){ root.next = next;//首先连接root,next if(root.left!=null&&root.right!=null){//如果不是最后一层的节点 helper(root.left,root.right);//连接左右节点 if(next!=null){//如果next不为空 helper(root.right,next.left);//连接root的右节点和next的左节点 }else{//如果next为空 helper(root.right,null);//root的右节点应该连接null } } }}
解法3,:该题的思路在于,要连接一个节点的右节点,和它相邻节点的左节点,所有我们扫描一个节点的时候,要知道其相邻节点。上面的递归方法,使用了参数的形式来记录相邻节点。在层次遍历中,我们手动记录相邻节点即可。另外,还要记录每层的第一个节点,从而省去队列。
/** * 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; TreeLinkNode cur = root;//当前节点 while(root.left!=null){//没有到最后一层的第一个节点 cur = root; while(cur!=null){//遍历当前层 cur.left.next = cur.right; if(cur.next!=null) cur.right.next = cur.next.left;//如果有右边的节点,设置 cur = cur.next; } root = root.left;//当前层第一个节点 } }}
解法4:思路和解法3一样
/** * 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; TreeLinkNode cur = root;//当前节点 TreeLinkNode next = null;//当前节点的右边节点 TreeLinkNode level = root.left;//下一层的第一个节点 cur.next = next; while(cur!=null){//没有到最后一个节点 if(cur.left!=null&&cur.right!=null){//如果不是最后一层 cur.left.next = cur.right;//连接左右节点 if(next!=null){//如果相邻节点不为空 cur.right.next = next.left;//连接当前节点的右节点,和相邻节点的左节点 }else{//否则 cur.right.next = null;//当前节点的右节点连接null } } if(next!=null){//如果相邻节点不为空 cur = next;//下一个节点 next = next.next;//下一个相邻节点 }else{//否则,说明该层遍历完毕 cur = level;//当前节点为下一个的第一个节点 if(cur!=null){//如果不是最后一层 level = cur.left;//更新level next = cur.next;//更新next } } } }}
- 【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
- 深入了解MyBatis参数
- hdu1226 超级密码(bfs+余数判重)
- mysql新手学习
- Android中的Service 与 Thread 的区别?
- leetcode | String to Integer (atoi)
- leetcode--Populating Next Right Pointers in Each Node
- Windows Phone 8.1中圆形图片或头像的制作、优化以及Stretch的四个属性值的区别
- 数据结构与算法——链表(1)
- 第十五周 项目二:二进制文件浏览器
- 使用Cocos Studio创建一个简单的工程
- Windows各个系列的服务器版本
- 查看日志分享2
- vim秒变成source insight,内附安装脚本和资源链接
- win2012 64位系统 iis8支持32位数据库