116. 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 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,

解题方法

BFS:

     BFS O\(n\) space O\(n\) 

     替child 牽線

紀錄Level:

//BFS O(n) space O(n)
//替child 牽線
    public void connect(TreeLinkNode root){
        //corner case
        if(root == null){
            return;
        }

        Queue<TreeLinkNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i = 0; i < size; i++){
                TreeLinkNode node = queue.poll();

                //next level
                if(node.left != null){
                    queue.offer(node.left);
                    node.left.next = node.right;
                }

                if(node.right != null){
                    queue.offer(node.right);
                    node.right.next = (i == size - 1) ? null : node.next.left;
                }
            }
        }
    }

    /*public void connect(TreeLinkNode root) {
        TreeLinkNode curr = root;
        while(curr != null){
            TreeLinkNode head = curr;
            while( curr != null){//當層跑完
                if(curr.left != null){
                    curr.left.next = curr.right;
                }
                if(curr.right != null){
                    curr.right.next = curr.next == null? null: curr.next.left;
                }
                curr = curr.next;
            }
            curr = head.left;
        }
    }*/

117. Populating Next Right Pointers in Each Node II

题目描述

解题方法

/需注意考慮child節點中間空白需多空節點

//需注意考慮child節點中間空白需多空節點
     public void connect(TreeLinkNode root){
        //corner case
        if(root == null){
            return;
        }

        Queue<TreeLinkNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i = 0; i < size; i++){
                TreeLinkNode node = queue.poll();
                //next level
                if(node.left != null){
                    queue.offer(node.left);
                    node.left.next = (i == size - 1) ? (node.right != null)? node.right : null
                        : (node.right != null)? node.right : connectNextNode(node.next);
                }
                if(node.right != null){
                    queue.offer(node.right);
                    node.right.next = (i == size - 1) ? null : connectNextNode(node.next);
                }
            }
        }
    }

    private TreeLinkNode connectNextNode(TreeLinkNode root){
        while(root != null){
            if(root.left != null){
                return root.left;
            }else if(root.right != null){
                return root.right;
            }else{
                root = root.next;
            }
        }
        return root;
    }




    //Using another pointer
    //Input:  {1,2,3,4,#,#,5}
    /*public void connect(TreeLinkNode root) {
        TreeLinkNode dummyHead = new TreeLinkNode(0);
        TreeLinkNode pre = dummyHead;
        while (root != null) {
            if (root.left != null) {
                pre.next = root.left;
                pre = pre.next;
            }
            if (root.right != null) {
                pre.next = root.right;
                pre = pre.next;
            }
            root = root.next;
            if (root == null) {
                pre = dummyHead;
                root = dummyHead.next;
                dummyHead.next = null;
            }
        }
    }*/

results matching ""

    No results matching ""