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

    解决思路

    层次遍历。

    开始节点为根节点,下一层为节点的左子结点。

    连接的过程中注意提前判断节点非空。

    程序

    public class Solution {
        public void connect(TreeLinkNode root) {
            if (root == null) {
                return ;
            }
    
            TreeLinkNode curLevel = root;
    
            while (curLevel != null) {
                TreeLinkNode cur = curLevel;
                while (cur != null) {
                    TreeLinkNode next = cur.next;
                    if (cur.left != null && cur.right != null) {
                        cur.left.next = cur.right;
                    }
                    if (cur.right != null && next != null) {
                        cur.right.next = next.left;
                    }
                    cur = next;
                }
                curLevel = curLevel.left;
            }
        }
    }
    

    Follow up

    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
    

    程序

    public class Solution {
        public void connect(TreeLinkNode root) {
            if (root == null) {
                return ;
            }
            
            TreeLinkNode curLevel = root;
            
            while (curLevel != null) {
                TreeLinkNode cur = curLevel;
                TreeLinkNode nextLevel = null; // mark the next node
                while (cur != null) {
                    TreeLinkNode next = cur.next;
                    while (next != null && next.left == null && next.right == null) {
                        next = next.next; // skip node with no children
                    }
                    if (nextLevel == null) {
                        // mark the first no-empty node as the next node
                        if (cur.left != null) {
                            nextLevel = cur.left;
                        } else if (cur.right != null) {
                            nextLevel = cur.right;
                        }
                    }
                    if (cur.left != null && cur.right != null) {
                        cur.left.next = cur.right;
                    }
                    if (next != null) {
                        // connect cross the subtrees
                        if (cur.right != null) {
                            cur.right.next = next.left == null ? next.right : next.left;
                        } else if (cur.left != null) {
                            cur.left.next = next.left == null ? next.right : next.left;
                        }
                    }
                    cur = next;
                }
                curLevel = nextLevel;
            }
        }
    }
    

      

  • 相关阅读:
    LODOP、C-LODOP注册号的区别
    Lodop强制分页LODOP.NewPage()和LODOP.NewPageA()
    c-lodop云打印实现手机打印 JS语句打印
    如何取消浏览器护眼色 Lodop打印图片有窗口颜色的边框
    PS中如何提高修改psd图片的效率(自动选择工具)
    Lodop如何打印直线
    Lodop打印控件 如何打印虚线
    Lodop窗口的按钮、权限,隐藏或设置功能不可用
    ArrayList与LinkedList区别
    URLDecoder与URLEncoder
  • 原文地址:https://www.cnblogs.com/harrygogo/p/4655295.html
Copyright © 2011-2022 走看看