zoukankan      html  css  js  c++  java
  • Java实现 LeetCode 117 填充每个节点的下一个右侧节点指针 II(二)

    117. 填充每个节点的下一个右侧节点指针 II

    给定一个二叉树

    struct Node {
      int val;
      Node *left;
      Node *right;
      Node *next;
    }
    

    填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。

    初始状态下,所有 next 指针都被设置为 NULL。

    进阶:

    你只能使用常量级额外空间。
    使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。

    示例:

    输入:root = [1,2,3,4,5,null,7]
    输出:[1,#,2,3,#,4,5,7,#]
    解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。

    提示:

    树中的节点数小于 6000
    -100 <= node.val <= 100

    
    class Solution {
       public Node connect_1(Node root) {
            if (root == null) {
                return null;
            }
            // 借助队列实现层次遍历
            LinkedList<Node> queue = new LinkedList<>();
            queue.add(root);
            while (!queue.isEmpty()) {
                int size = queue.size();
                while (size-- > 0) {
                    Node node = queue.remove();
                    if (size > 0) {
                        node.next = queue.peek();
                    }
                    if (node.left != null) {
                        queue.add(node.left);
                    }
                    if (node.right != null) {
                        queue.add(node.right);
                    }
                }
            }
            return root;
        }
    
        public Node connect(Node root) {
            if (root == null) {
                return null;
            }
            if (root.left != null) {
                if (root.right != null) {
                    // 若右子树不为空,则左子树的 next 即为右子树
                    root.left.next = root.right;
                } else {
                    // 若右子树为空,则右子树的 next 由根节点的 next 得出
                    root.left.next = nextNode(root.next);
                }
            }
            if (root.right != null) {
                // 右子树的 next 由根节点的 next 得出
                root.right.next = nextNode(root.next);
            }
            // 先确保 root.right 下的节点的已完全连接,因 root.left 下的节点的连接
            // 需要 root.left.next 下的节点的信息,若 root.right 下的节点未完全连
            // 接(即先对 root.left 递归),则 root.left.next 下的信息链不完整,将
            // 返回错误的信息。可能出现的错误情况如下图所示。此时,底层最左边节点将无
            // 法获得正确的 next 信息:
            //                  o root
            //                 / 
            //     root.left  o —— o  root.right
            //               /    / 
            //              o —— o   o
            //             /        / 
            //            o        o   o
            connect(root.right);
            connect(root.left);
            return root;
        }
    
        private Node nextNode(Node node) {
            while (node != null) {
                if (node.left != null) {
                    return node.left;
                }
                if (node.right != null) {
                    return node.right;
                }
                node = node.next;
            }
            return null;
        }
    
    }
    
  • 相关阅读:
    HTML滚动时位置固定 PHP
    js判断验证码是否正确 PHP
    PNG渐变图生成工具 PHP
    C# 提醒小工具 PHP
    js 密码强度检测 PHP
    js辅助输入层 PHP
    不常用样式 PHP
    ASP.NET编程中的十大技巧
    WEB打印大全
    如何在ASP.NET中用OWC绘制图表
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13075531.html
Copyright © 2011-2022 走看看