zoukankan      html  css  js  c++  java
  • 116. Populating Next Right Pointers in Each Node

    题目:

    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

    链接:  http://leetcode.com/problems/populating-next-right-pointers-in-each-node/

    4/16/2017

    BB电面准备

    抄答案的,递归方法,然而并不是constant extra space

     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         if (root.right != null) {
    13             root.left.next = root.right;
    14             if (root.next != null) {
    15                 root.right.next = root.next.left;
    16             }
    17         }
    18         connect(root.left);
    19         connect(root.right);
    20         return;
    21     }
    22 }

    iterative,注意最后一步中需要head.next = null。一旦缺少这一步,cur->next在判断到root在最后一层时依然保持着这一层的next,会直接跳过前两个判断直接到root == null的判断,无限循环。有了head.next = null,当root为叶节点时,当判断完这一层root = cur.next,变为0,跳出外层循环。

    head是root下一层的头, cur在root下一层按照next顺序来遍历。

     1 public class Solution {
     2     public void connect(TreeLinkNode root) {
     3         if (root == null) return;
     4         TreeLinkNode cur = new TreeLinkNode(-1);
     5         TreeLinkNode head = cur;
     6         
     7         while (root != null) {
     8             if (root.left != null) {
     9                 cur.next = root.left;
    10                 cur = cur.next;
    11             }
    12             if (root.right != null) {
    13                 cur.next = root.right;
    14                 cur = cur.next;
    15             }
    16             root = root.next;
    17             if (root == null) {
    18                 cur = head;
    19                 root = head.next;
    20                 head.next = null;
    21             }
    22         }
    23         return;
    24     }
    25 }

    其他解法:

    https://discuss.leetcode.com/topic/2202/a-simple-accepted-solution

     1 void connect(TreeLinkNode *root) {
     2     if (root == NULL) return;
     3     TreeLinkNode *pre = root;
     4     TreeLinkNode *cur = NULL;
     5     while(pre->left) {
     6         cur = pre;
     7         while(cur) {
     8             cur->left->next = cur->right;
     9             if(cur->next) cur->right->next = cur->next->left;
    10             cur = cur->next;
    11         }
    12         pre = pre->left;
    13     }
    14 }

    更多讨论:

    https://discuss.leetcode.com/category/124/populating-next-right-pointers-in-each-node

  • 相关阅读:
    打造自己的LINQ Provider(上):Expression Tree揭秘
    asp.net MVC 开篇
    .net面试基础
    asp.net MVC3.0
    数字万用表的四位半,三位半都是什么意思?
    lpsz,sz, 变量的命名规范
    老毛桃PE系统安装篡改主页3456.com和强制安装绿色浏览器lvseie.exe
    PC电源厂商及品牌篇(台厂及国际品牌篇)(第二版)
    Borland C++ 语法摘要
    Win8打开文件夹卡顿
  • 原文地址:https://www.cnblogs.com/panini/p/6721094.html
Copyright © 2011-2022 走看看