关闭

Populating Next Right Pointers in Each Node II (hard)

标签: Populating Next Righ
110人阅读 评论(0) 收藏 举报
分类:

【题目】

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

【题意】

      如果有右孩子,则使其next为右孩子,否则为null,但是本身是任意二叉树不满足是满二叉树

【分析】

    

【实现】

/**
 * 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){  
            return;  
        }  
           
        // 找到与root同一行的next node  
        TreeLinkNode rootNext = root.next;  
        TreeLinkNode next = null;       // 下一个被连接的对象  
           
        // rootNext如果是null说明已经处理完这一层的所有node  
        // next不等于null说明找到了找到最左边的下一个被连接的对象  
        while (rootNext != null && next == null)  
        {  
            if (rootNext.left != null){ // 优先找左边  
                next = rootNext.left;  
            } else{  
                next = rootNext.right;  
            }  
            rootNext = rootNext.next;  
        }  
    
        if (root.left != null)  
        {  
            if (root.right != null){    //  内部相连  
                root.left.next = root.right;  
            }else{                      // 跨树相连  
                root.left.next = next;  
            }  
        }  
        if (root.right != null){        // 跨树相连  
            root.right.next = next;  
        }  
           
        connect(root.right);        // 要先让右边都先连起来  
        connect(root.left);  
        
    }
}


0
0
查看评论

Populating Next Right Pointers in Each Node II 任意(非完美)二叉树添加next指针 @LeetCode

这道题有些tricky,有时间回头再研究一下 package Level4; import Utility.TreeLinkNode; /** * Populating Next Right Pointers in Each Node II * * Follow up for p...
  • hellobinfeng
  • hellobinfeng
  • 2013-11-21 04:45
  • 3390

[leetcode]117. Populating Next Right Pointers in Each Node II

注意一个事实:到根节点的左子节点的next要么指向根节点的右子节点(右子节点不为空),要么就指向根节点的next(或者next的next)节点p,直到该p节点具有下一代的节点。也就是说,将每一层的next链上的节点视为兄弟关系,那么子辈的兄弟必然是父辈兄弟的子辈。基于以上事实,采用DFS,注意右子树...
  • zorelemn
  • zorelemn
  • 2016-04-14 16:13
  • 278

Hard-题目6:117. Populating Next Right Pointers in Each Node II

题目原文: Follow up for problem “Populating Next Right Pointers in Each Node”.What if the given tree could be any binary tree? Would your previous soluti...
  • cmershen
  • cmershen
  • 2016-05-31 23:24
  • 298

[LeetCode]Populating Next Right Pointers in Each Node II, 解题报告

前言 今天上午从9点半开始就做了这么一道题目,还是挺有意思的,这里记录一下 题目 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tr...
  • zinss26914
  • zinss26914
  • 2013-12-19 11:17
  • 7606

【LeetCode-面试算法经典-Java实现】【116-Populating Next Right Pointers in Each Node(二叉树链接右指针)】

【116-Populating Next Right Pointers in Each Node(二叉树链接右指针)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree struct TreeLinkNode { Tr...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-12 06:30
  • 3053

Populating Next Right Pointers in Each Node -- LeetCode

原题链接: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/  这道题是要将一棵树的每一层维护成一个链表,树本身是给定的。其实思路上很接近层序遍历Binary Tree ...
  • linhuanmars
  • linhuanmars
  • 2014-04-12 03:11
  • 10275

【LeetCode-面试算法经典-Java实现】【117-Populating Next Right Pointers in Each Node(二叉树链接右指针II)】

【117-Populating Next Right Pointers in Each Node(二叉树链接右指针II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Follow up for problem “Populating Next Right Pointe...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-13 06:18
  • 2002

Leetcode 树 Populating Next Right Pointers in Each Node II

题意:给定一棵任意二叉树(不一定是perfect binary tree),将它每一个节点的next指针都指向该节点右边的节点 思路:bfs 这里不能用dfs了,只能用bfs bfs遍历将同一层的节点存放在同一个数组里, 然后在遍历每个数组,将前面的节点和后面的节点connect起来, 最后一个节点...
  • zhsenl
  • zhsenl
  • 2014-05-13 22:19
  • 1030

[leetcode] 117. Populating Next Right Pointers in Each Node II 解题报告

题目链接:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem "Populating Next Right Pointers in Eac...
  • qq508618087
  • qq508618087
  • 2016-01-13 06:36
  • 985

[LeetCode]117.Populating Next Right Pointers in Each Node II

【题目】 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your pre...
  • SunnyYoona
  • SunnyYoona
  • 2014-12-24 19:05
  • 1246
    个人资料
    • 访问:26654次
    • 积分:946
    • 等级:
    • 排名:千里之外
    • 原创:58篇
    • 转载:70篇
    • 译文:1篇
    • 评论:1条