leetcode--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 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

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

public class Solution {

    public
void connect(TreeLinkNode root) {

        if(root != null){

            root.next = null;

            TreeLinkNode topLevel = root;

            TreeLinkNode nextLevelHead = null;

            TreeLinkNode node = null;

            while(topLevel != null){

                //looking for the head of next level

                while(topLevel != null
&& nextLevelHead == null){

                    if(topLevel.left != null){

                        nextLevelHead = topLevel.left;

                        node = nextLevelHead;

                    }

                    if(topLevel.right != null){

                        if(node == null){

                            nextLevelHead = topLevel.right;

                            node = nextLevelHead;

                        }

                        else{

                            node.next = topLevel.right;

                            node = node.next;

                        }

                    }

                    

                    topLevel = topLevel.next;

                }

                

                while(topLevel != null){

                    if(topLevel.left != null){

                        node.next = topLevel.left;

                        node = node.next;

                    }

                    if(topLevel.right != null){

                        node.next = topLevel.right;

                        node = node.next;

                    }

                    topLevel = topLevel.next;

                }

                

                if(node != null){

                    node.next = null;

                    node = node.next;

                }

                topLevel = nextLevelHead;

                nextLevelHead = null;

            }

        }

    }

}

  

leetcode--Populating Next Right Pointers in Each Node
II,布布扣,bubuko.com

leetcode--Populating Next Right Pointers in Each Node
II

时间: 2024-10-09 12:56:09

leetcode--Populating Next Right Pointers in Each Node II的相关文章

LeetCode——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 solution still work? Note: You may only use constant extra space. For example, Given the following binary t

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

[题目] 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 bin

LeetCode: Populating Next Right Pointers in Each Node II 解题报告

Populating Next Right Pointers in Each Node IIFollow 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 sp

[LeetCode] [LeetCode] 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 solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

[LeetCode] 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 solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

LeetCode - Populating Next Right Pointers in Each Node II 及其变形题

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, al

[leetcode] 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, al

[leetcode]Populating Next Right Pointers in Each Node @ Python

原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ 题意: 1 / 2 3 / \ / 4 5 6 7变为: 1 -> NULL / 2 -> 3 -> NULL / \ / 4->5->6->7 -> NULL 解题思路:看到二叉树我们就想到需要使用递归的思路了.直接贴代码吧,思路不难. 代码: # Definition for a binary t

Populating Next Right Pointers in Each Node II leetcode java

题目: 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 bina

Leetcode 树 Populating Next Right Pointers in Each Node II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II Total Accepted: 9695 Total Submissions: 32965 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could