• 117. 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
    

    同116题类似,用中序遍历的话,需要O(n)的时间复杂度。
    通过前一行的next,来找下一行的next。用两个指针prevLevelStart和prev, 分别指向前一行有孩子的第一个节点,和用来连接当前行时、前一行的辅助节点。
    与116相比,只是updatePrevLevelStart和updatePrev的规则变了。
    对116由于是perfect binary tree:
         prevLevelStart = prevLevelStart.left
         prev = prev.next

    对117题,要跳过空节点。 30%

    /**
     * Definition for binary tree with next pointer.
     * public class TreeLinkNode {
     *     int val;
     *     TreeLinkNode left, right, next;
     *     TreeLinkNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public void connect(TreeLinkNode root) {
            TreeLinkNode prevLevelStart = root, prev, cur;
            while (prevLevelStart != null) {
                prev = prevLevelStart;
                if (prev.left != null) {
                    cur = prev.left;
                } else {
                    cur = prev.right;
                    prev = updatePrev(prev);
                }
                while (prev != null) {
                    if (cur == prev.left) {
                        if (prev.right != null) {
                            cur.next = prev.right;
                            cur = cur.next;
                        }
                        prev = updatePrev(prev);   
                    } else {
                        if (prev.left != null) {
                            cur.next = prev.left;
                            cur = cur.next;
                        } else {
                            cur.next = prev.right;
                            cur = cur.next;
                            prev = updatePrev(prev);
                        }
                    }
                }
                prevLevelStart = updatePrevLevelStart(prevLevelStart);
            }
        }
        public TreeLinkNode updatePrev(TreeLinkNode prev) {
            prev = prev.next;
            while (prev != null && prev.left == null && prev.right == null) {
                prev = prev.next;
            }
            return prev;
        }
        public TreeLinkNode updatePrevLevelStart(TreeLinkNode prevLevelStart) {
            while (prevLevelStart != null) {
                if (prevLevelStart.left != null && (prevLevelStart.left.left != null || prevLevelStart.left.right != null)) {
                    return prevLevelStart.left;
                } else if (prevLevelStart.right != null && (prevLevelStart.right.left != null || prevLevelStart.right.right != null)) {
                    return prevLevelStart.right;
                } else {
                    prevLevelStart = prevLevelStart.next;
                }
            }
            return null;
        }
    }
    
  • 相关阅读:
    IOS开发报错之Undefined symbols for architecture armv6
    企业级证书使用帮助
    使用Hybris的customer coupon进行促销活动(promotion)
    Hybris Storefront里产品图片显示不出来的分析方法
    HubSpot company数据在UI上的展示和通过API方式进行获取
    有些CRM settype用事务码COMM_ATTRSET打不开的原因
    Global variable in ABAP function group
    CRM product model的用法
    How to prove that SAP CRM WebUI is a stateful application
    CRM产品主数据在行业解决方案industry solution中的应用
  • 原文地址:https://www.cnblogs.com/yuchenkit/p/7192625.html
Copyright © 2020-2023  润新知