本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43532817


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

思路:

(1)题意为给定一颗二叉树(可以当做满二叉树),将其每层上的节点从左到右链接起来形成链表。

(2)本文主要使用队列来实现。大体思路为:从根节点开始,按从树根向下的顺序依次将每层的节点存入队列中。首先,将根节点存入队列中,只要队列不为空,就让队头元素出队,然后判断该元素是否有左右孩子,如果有分别将左右孩子加入队列中;然后,二叉树是满二叉树,所以可以求出每层的节点数目,依次为1,2,4,8,....,通过临时变量记录当前的层次level,通过Math.power(2, level),求得当前层上的节点数levelcount,还需要使用变量count记录当前层次上遍历了多少节点,这样每当遍历的节点数和levelcount相同时,说明当前层已遍历到最后一个节点,将该节点的next置为null,继续进行下一层的遍历。由于每次出队的总是第一个元素,所以只需将出队后的元素next置为当前队列第一个元素即可;循环遍历,直到所有节点进队列、出队列为止,最后所得即为结果。

(3)该题的思路和“按层次遍历二叉树”类似,感兴趣可以参照“按层次输出二叉树”。只不过本文所示算法效率不是很高,但是比较容易理解吧。

(4)希望本文对你有所帮助。

算法代码实现如下:

/**
 *
 * @author liqq
 */
public static void connect(TreeLinkNode root) {
	if(root ==null) return ;
	if(root!=null&&root.left==null&&root.right==null) root.next=null;

	List<TreeLinkNode> list = new LinkedList<TreeLinkNode>();
	list.add(root);
	int count = 0;
	int level = 0;
	int levelcount = 1;
	while(list.size()!=0){
		TreeLinkNode fis = list.remove(0);
		count++;
		if(list.size()>=0 && count!=levelcount){
			TreeLinkNode sec = list.get(0);
			fis.next = sec;
		}else{
			fis.next =null;
			level++;
			levelcount = (int) Math.pow(2, level);
			count=0;
		}

		if(fis.left!=null){
			list.add(fis.left);
		}

		if(fis.right!=null){
			list.add(fis.right);
		}
	}
}

Leetcode_116_Populating Next Right Pointers in Each Node的更多相关文章

  1. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  2. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  3. [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 ...

  4. [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  5. LEETCODE —— Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

  6. LeetCode - 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】Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

  8. 【leetcode】Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

  9. 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

随机推荐

  1. 潜谈IT从业人员在传统IT和互联网之间的择业问题(下)-互联网公司

    互联网带来的一片晴天 相对于传统行业来说,互联网行业要显得相对对技术人员尊重些. 在互联网行业中,采用的技术.概念也较传统形行业来说要新,技术人员也容易在此找到自己的一方净土. 因为互联网这个行当讲究 ...

  2. Spark核心类:弹性分布式数据集RDD及其转换和操作pyspark.RDD

    http://blog.csdn.net/pipisorry/article/details/53257188 弹性分布式数据集RDD(Resilient Distributed Dataset) 术 ...

  3. Dynamics CRM 权限整理二

    接上篇http://blog.csdn.net/vic0228/article/details/50510605,继续列举CRM相关权限 prvReadBusinessUnit privilege(I ...

  4. FORM开发技术之动态控制某些item的属性

    利用FORM内置函数控制ITEM包括按钮,普通ITEM等等的属性,更多内置函数学习课参考我的博客FORM内置系统函数 http://blog.csdn.net/cai_xingyun/article/ ...

  5. PGM:概率论基础知识

    http://blog.csdn.net/pipisorry/article/details/52459847 概率图模型PGM:概率论基础知识 独立性与条件独立性 独立性 条件独立性 也就是表示给定 ...

  6. FFmpeg的HEVC解码器源代码简单分析:CTU解码(CTU Decode)部分-PU

    ===================================================== HEVC源代码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpe ...

  7. Android之Animation动画的使用(一)

    我们在使用一些控件时候,难免会设置一些进入和退出的动画效果,比如popupwindow.listview的item动画.按钮.图片等等,要使这些控件有动画效果,当然需要用到Animation了. 下面 ...

  8. 05_MyBatis基于注解的开发

     要想开发基于注解的MyBatis应用.需要先写一个带有注解的接口. PersonDao.java的写法如下: package com.rl.dao; import java.util.List; ...

  9. SDL2源代码分析2:窗口(SDL_Window)

    ===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDL_Init()) SDL ...

  10. 03一些View总结

    第三天 一  TextView    父类 : View     >概念:文本控件 :文本内容的显示   默认配置不可编辑  子类EditText可以编辑          >属性:    ...