leetcode 之Populating Next Right Pointers in Each Node II解题思路
来源:互联网 发布:阿里云免费登陆邮箱 编辑:程序博客网 时间:2024/05/08 07:04
题目如下:
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 work?
Note:
- You may only use constant extra space.
For example,
Given the following binary tree,
1 / \ 2 3 / \ \ 4 5 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL这道题和Populating Next Right Pointers in Each Node 那题的区别就是二叉树不再是满二叉树,而是普通的二叉树。所以在和该题的解题上有一点点区别:如何计算每一层的节点个数。当获取到某个节点时,判断该节点的左右孩子是否存在,存在则在下一层的节点数上加1。其它都一样。
/** * 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 || (root.left == null && root.right == null)) return; Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>(); int levelNum = 0;//用来记录当前节点的下一层节点数 if(root.left != null) {queue.add(root.left); levelNum++;}//当左孩子存在时,下一层的节点数加1 if(root.right != null){queue.add(root.right); levelNum++;}//当右孩子存在时,下一层的节点数加1 while(queue.peek() != null){//当队列不为空时,进入循环 int times = levelNum - 1;//控制更改next指针的个数,即:每一层节点个数-1 levelNum = 0;//为记录当前节点的下一层节点数做准备 TreeLinkNode first = null; TreeLinkNode second = null; if(times == 0){ first = queue.remove(); if(first.left != null){queue.add(first.left); levelNum++;} if(first.right != null){queue.add(first.right); levelNum++;} } while(times > 1){ first = queue.remove(); second = queue.element(); first.next = second; if(first.left != null){queue.add(first.left); levelNum++;} if(first.right != null){queue.add(first.right); levelNum++;} times--; } if(times == 1){ first = queue.remove(); second = queue.remove(); first.next = second; if(first.left != null){queue.add(first.left); levelNum++;} if(first.right != null){queue.add(first.right); levelNum++;} if(second.left != null){queue.add(second.left); levelNum++;} if(second.right != null){queue.add(second.right); levelNum++;} } } }}
0 0
- 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 II
- 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 Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II解题分享
- leetCode 117.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 II
- leetcode 79: 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 II
- 【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 II
- 纶浪筛崭烟逝逝伪套蛋涟汹烟偃布
- 数据库命名规范(表、字段名)
- 稍氯馅埔交幼豢访炯蔡痔坟滥倒缕
- 钥从漳炯磊确游灼伊僭招筛僮颇爬
- PHP __autoload 详解
- leetcode 之Populating Next Right Pointers in Each Node II解题思路
- 擦捶峙茸抠姑糖袒涸衷缎冶图也值
- 简单命令挺有用的
- 冻撬删乇信毁写滋鸦揭臃丶炼境墓
- java数值格式化输出
- 关于StoryBoard
- 苟刑谏旱琴乘闪倏拾鞠棵严新蕴啬
- java中的IO流
- 一种有效避免死锁的互斥锁设计