zoukankan      html  css  js  c++  java
  • [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 toNULL.

    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  * struct TreeLinkNode {
     4  *  int val;
     5  *  TreeLinkNode *left, *right, *next;
     6  *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     void convert(TreeLinkNode *root)
    12     {
    13         if (root == NULL)
    14             return;
    15             
    16         TreeLinkNode *leftNode = root->left;
    17         TreeLinkNode *rightNode = root->right;
    18         
    19         if (leftNode && rightNode)
    20         {
    21             leftNode->next = rightNode;
    22             rightNode->next = NULL;
    23             TreeLinkNode *rightChild = leftNode->right;
    24             TreeLinkNode *leftChild = rightNode->left;
    25             if (leftChild && rightChild)
    26                 leftChild->next = rightChild;                
    27         }
    28         
    29         connect(leftNode);
    30         connect(rightNode);        
    31     }
    32     
    33     void fix(TreeLinkNode *leftNode, TreeLinkNode *rightNode)
    34     {
    35         if (leftNode == NULL)
    36             return;
    37             
    38         leftNode->next = rightNode;
    39         
    40         fix(leftNode->right, rightNode->left);
    41     }
    42     
    43     void connect(TreeLinkNode *root) {
    44         // Start typing your C/C++ solution below
    45         // DO NOT write int main() function
    46         if (root == NULL)
    47             return;
    48             
    49         convert(root);
    50         fix(root->left, root->right);
    51     }
    52 };
  • 相关阅读:
    杨晓峰-Java核心技术-6 动态代理 反射 MD
    ARouter 路由 组件 跳转 MD
    领扣-5 最长回文子串 Longest Palindromic Substring MD
    算法 递归 迭代 动态规划 斐波那契数列 MD
    二叉树 遍历 先序 中序 后序 深度 广度 MD
    算法 数组中出现次数最多的数字 MD
    领扣-754 到达终点数字 Reach a Number MD
    领扣-1/167 两数之和 Two Sum MD
    文件 File 常见操作 工具 MD
    IO流 简介 总结 API 案例 MD
  • 原文地址:https://www.cnblogs.com/chkkch/p/2787533.html
Copyright © 2011-2022 走看看