树——populating-next-right-pointers-in-each-node(填充每个节点的next指针)

问题

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 toNULL.

Initially, all next pointers are set toNULL.

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

思路

首先判断根节点是否为null,如果是,return;如果不是,继续。

设置节点node,用来指向每层的第一个节点;设置节点next,用来指向该层的每个节点。

当node的左孩子存在时,使用next来遍历该层的其它节点:

  (1)让node左孩子的next指向node右孩子;

  (2)若next.next不为null,让node右孩子的next指向next.next的左孩子。

代码

 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)
12             return;
13         root.next = null;
14         TreeLinkNode node = root, next = node;
15         while(node.left!=null){
16             next = node;
17             while(next!=null){
18                 next.left.next = next.right;
19                 if(next.next!=null)
20                     next.right.next = next.next.left;
21                 next = next.next;
22             }
23             node = node.left;
24         }
25     }
26 }
时间: 2024-10-10 22:34:21

树——populating-next-right-pointers-in-each-node(填充每个节点的next指针)的相关文章

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

Leetcode 树 Populating Next Right Pointers in Each Node

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node Total Accepted: 13657 Total Submissions: 39540 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }

[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_116题——Populating Next Right Pointers in Each Node(树,广度优先搜索)

Populating Next Right Pointers in Each Node Total Accepted: 49664 Total Submissions: 137310My Submissions Question Solution Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next po

leetcode 117 Populating Next Right Pointers in Each Node II ----- 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 binary tr

LeetCode[Tree]: 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][JavaScript]Populating Next Right Pointers in Each Node

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

[Leetcode][JAVA] 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

【57】117. Populating Next Right Pointers in Each Node II

117. Populating Next Right Pointers in Each Node II Description Submission Solutions Add to List Total Accepted: 84339 Total Submissions: 251330 Difficulty: Medium Contributors: Admin Follow up for problem "Populating Next Right Pointers in Each Node

【LeetCode】 Populating Next Right Pointers in Each Node 完全二叉树

题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * LeetCode Populating Next Right Pointers in Each Node * 题目:为树的每个节点增加一个next指针,指向树状结构排列时自己的右边节点,如果右边没有节点则置为null * * Definition for binary tree with next pointer