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问题的基础上,难度20,方法一样。都是类似Binary Tree Level Order Traverse,都是把树看成一个无向图,然后用BFS的方式,需要记录每一层的ParentNumInQueue以及ChildNumInQueue, 初始值为1和0,以后每次ParentNumInQ减至0说明这一层已经遍历完毕,这一层的Child数将成为下一层的ParentNumInQ

 1 /**
 2  * Definition for binary tree with next pointer.
 3  * public class TreeLinkNode {
 4  *     int val;
 5  *     TreeLinkNode left, right, next;
 6  *     TreeLinkNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public void connect(TreeLinkNode root) {
11         if (root == null) return;
12         LinkedList<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
13         queue.add(root);
14         int ParentNumInQ = 1;
15         int ChildNumInQ = 0;
16         TreeLinkNode pre = null;
17         while (!queue.isEmpty()) {
18             TreeLinkNode cur = queue.poll();
19             ParentNumInQ--;
20             if (pre == null) {
21                 pre = cur;
22             }
23             else {
24                 pre.next = cur;
25                 pre = pre.next;
26             }
27             if (cur.left != null) {
28                 queue.add(cur.left);
29                 ChildNumInQ++;
30             }
31             if (cur.right != null) {
32                 queue.add(cur.right);
33                 ChildNumInQ++;
34             }
35             if (ParentNumInQ == 0) {
36                 ParentNumInQ = ChildNumInQ;
37                 ChildNumInQ = 0;
38                 pre.next = null;
39                 pre = null;
40             }
41         }
42     }
43 }

层次递进法

复杂度

时间 O(N) 空间 O(1)

 1 public class Solution {
 2
 3     //based on level order traversal
 4     public void connect(TreeLinkNode root) {
 5
 6         TreeLinkNode head = null; //head of the next level
 7         TreeLinkNode prev = null; //the leading node on the next level
 8         TreeLinkNode cur = root;  //current node of current level
 9
10         while (cur != null) {
11
12             while (cur != null) { //iterate on the current level
13                 //left child
14                 if (cur.left != null) {
15                     if (prev != null) {
16                         prev.next = cur.left;
17                     } else {
18                         head = cur.left;
19                     }
20                     prev = cur.left;
21                 }
22                 //right child
23                 if (cur.right != null) {
24                     if (prev != null) {
25                         prev.next = cur.right;
26                     } else {
27                         head = cur.right;
28                     }
29                     prev = cur.right;
30                 }
31                 //move to next node
32                 cur = cur.next;
33             }
34
35             //move to next level
36             cur = head;
37             head = null;
38             prev = null;
39         }
40
41     }
42 }

转载于:https://www.cnblogs.com/EdwardLiu/p/3978460.html

Leetcode: Populating Next Right Pointers in Each Node II相关推荐

  1. leetcode - Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  2. LeetCode Populating Next Right Pointers in Each Node II(dfs)

    问题:给出一个二叉查找树,将结点与其右边的结点相连 思路: 从顶向上,从右向左的方式 .递归过程中,在当前结点及父结点作为参数传递.在向下的过程中,如果父结点不为空则获取当前结点的next结点.如果当 ...

  3. 【To Understand!】LeetCode 117. Populating Next Right Pointers in Each Node II

    LeetCode 117. Populating Next Right Pointers in Each Node II Solution1:我的答案 层次遍历 /*** Definition for ...

  4. 117 Populating Next Right Pointers in Each Node II

    117 Populating Next Right Pointers in Each Node II 就是 Bibary Tree Level order Traverse class Solutio ...

  5. LeetCode 117. Populating Next Right Pointers in Each Node II

    原题链接在这里:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题目: Given a bi ...

  6. LeetCode OJ - Populating Next Right Pointers in Each Node II

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  7. [Leetcode][JAVA] Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  8. Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  9. 117. Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

最新文章

  1. Seetaface 向树莓派 移植
  2. 阿里云专家手把手教你重塑 IT 架构!
  3. VTK:Filtering之ConnectivityFilterDemo
  4. win7系统电脑自动重启解决方法
  5. Team Work(CF 932 E)[bzoj5093][Lydsy1711月赛]图的价值
  6. cocos2dx--cocos2dx3.1.1执行报无法解析的外部符号
  7. iOS 应用首次开启 出现引导页面
  8. 2008年度最佳开源CMS大奖赛开幕
  9. 【笔试/面试】—— Linux 查看 cpu 和内存使用情况
  10. 荣耀智慧屏搭载了鸿蒙os吗,荣耀智慧屏尝鲜鸿蒙OS “一招鲜”能否吃遍天
  11. redis数据类型命令
  12. 代码分析UEFI的执行流程
  13. 嵌入式车牌识别与称重系统
  14. 怎么用计算机算ess tss,ESS、RSS、TSS分别表示什么?
  15. 数字电视业务PSI/SI学习
  16. 膨胀卷积(DILATED CONVOLUTIONS)
  17. 如何挖到人生当中第一本CNVD
  18. 鞋底php是什么材质,鞋底用EVA材料更好还是橡胶呢?
  19. leetcode hot100 梳理
  20. 锁消除、锁粗化、偏向锁、适应性锁

热门文章

  1. 提高C++代码质量 - [083]不要返回局部变量的引用
  2. MySQL第4天:MySQL的架构介绍之修改数据库编码格式
  3. python二十六: 字符串颜色
  4. 令人头疼的clientTop、scrollTop、offsetTop
  5. GitHub 给安全行业的四大启示
  6. 查看Oracle耗时Sql
  7. 数据库连接池DBPool分析(一):简介
  8. OSI七层模型具体解释
  9. 打造轻量化的View Controller
  10. dll窗体的创建与调用