Populating Next Right Pointers in Each Node I & II 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 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

廣度優先搜索

思路

至關於層序遍歷BST, 只是除了每層的最後一個節點其餘節點都要給一個next指針指向隊列裏的下一個節點. 此方法I &II都適用node

複雜度

時間O(n) 空間O(n)spa

代碼

public void connect(TreeLinkNode root) {指針

Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
    if (root == null) {
        return;
    }
    queue.offer(root);
    while (!queue.isEmpty()) {
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            TreeLinkNode cur = queue.poll();
            if (i < size - 1) {//除了每層最後一個節點
                TreeLinkNode next = queue.peek();
                cur.next = next;
            }
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

遞歸解法

思路

node的left child 要指向right child, 若是node.next 是null的話, right child指向null, 若是不是null, right child則指向root.next的left childcode

複雜度

時間O(n) 空間O(1)遞歸

代碼

public void connect(TreeLinkNode root) {
    if (root == null) {
        return;
    }
    if (root.left != null) {
        root.left.next = root.right;
    }
    if (root.right != null) {
        if (root.next != null) {
            root.right.next = root.next.left;
        } else {
            root.right.next = null;
        }
    }
    connect(root.left);
    connect(root.right);
}

Populating Next Right Pointers in Each Node II

遞歸解法

思路

這道題的binary tree不必定是完整的 因此root.right.next指向是不定的 思路就是先找到root.right 右邊第一個可行node存儲
注意這道題應該先遍歷右邊而後再左邊隊列

複雜度

時間O(n) 空間O(1)it

代碼

public void connect(TreeLinkNode root) {
    if (root == null) {
        return;
    }
    TreeLinkNode tem = root.next;
    while (tem != null) {
        if (tem.left != null) {
            tem = tem.left;
            break;
        } else if (tem.right != null) {
            tem = tem.right;
            break;
        } else {
            tem = tem.next;
        }
    }
    if (root.right != null) {
        root.right.next = tem;
    }
    if (root.left != null) {
        if (root.right != null) {
            root.left.next = root.right;
        } else {
            root.left.next = tem;
        }
    }
    connect(root.right);//此處必須先構建right而後left
    connect(root.left);
}
相關文章
相關標籤/搜索