zoukankan      html  css  js  c++  java
  • leetcode------Populating Next Right Pointers in Each Node

    标题: Populating Next Right Pointers in Each Node
    通过率: 36.1%
    难度: 中等

    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  * 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         int count=1,tmp=0;
    12         LinkedList<TreeLinkNode> queue=new LinkedList<TreeLinkNode>();
    13         TreeLinkNode start=null,sec=null,pre=null;
    14         if(root!=null){
    15          queue.addLast(root);
    16         }
    17         while(!queue.isEmpty()){
    18             pre=queue.pollFirst();
    19                 if(count==1)pre.next=null;
    20                 if(pre.left!=null){
    21                     queue.addLast(pre.left);
    22                     tmp++;
    23                 }
    24                 if(pre.right!=null){
    25                     queue.addLast(pre.right);
    26                     tmp++;
    27                 }
    28 
    29             for(int i=1;i<count;i++){
    30                 start=queue.pollFirst();
    31                 pre.next=start;
    32                 pre=start;
    33                 if(start.left!=null){
    34                     queue.addLast(start.left);
    35                     tmp++;
    36                 }
    37                 if(start.right!=null){
    38                     queue.addLast(start.right);
    39                     tmp++;
    40                 }
    41             }
    42             count=tmp;
    43             tmp=0;
    44             if(start!=null)
    45             start.next=null;
    46         }
    47         
    48     }
    49 }
  • 相关阅读:
    android学习计划2
    在ubuntu12.04下编译android4.1.2添加JNI层出现问题
    android原生系统裁剪
    LM393,LM741可以用作电压跟随器吗?
    android-86-Can't create handler inside thread that has not called Looper.prepare()
    三星 PMU NXE2000,x-powers的AXP228,NXE2000
    当函数没有return时错误
    Perl OOP
    ORA-01031: 权限不足
    Oracle 10g 10.2.0.1 在Oracle Linux 5.4 32Bit RAC安装手冊(一抹曦阳)
  • 原文地址:https://www.cnblogs.com/pkuYang/p/4334761.html
Copyright © 2011-2022 走看看