Populating Next Right Pointers in Each Node II

来源:互联网 发布:农产品交易软件 编辑:程序博客网 时间:2024/05/20 06:09

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

这道题目用bfs 和 dfs 都可以做。更推荐bfs。如果用dfs ,则需要注意的 是 要先遍历右子树,再遍历左子树。因为对左子树的操作,用到了右子树的结果。
下面是我写的dfs,后面是答案的bfs

/** * 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) {        if (root == null) {            return;        }        root.next = null;        find(root, root.right);        find(root, root.left);    }    private void find(TreeLinkNode parent, TreeLinkNode cur) {        if (cur == null) {            return;        }        if (parent.right != null && cur != parent.right) {            cur.next = parent.right;        } else {            TreeLinkNode temp = parent.next;            while (temp != null) {                if (temp.left != null) {                   cur.next = temp.left;                   break;                } else if (temp.right != null) {                   cur.next = temp.right;                   break;                } else{                   temp = temp.next;                }             }             if (temp == null) {                 cur.next = null;             }        }        if (cur.right != null) {            find(cur, cur.right);        }        if (cur.left != null) {            find(cur, cur.left);        }            }}



public class Solution {    public void connect(TreeLinkNode root) {        if (root == null) {            return;        }        TreeLinkNode parent = root;        TreeLinkNode pre;        TreeLinkNode next;        while (parent != null) {            pre = null;            next = null;            while (parent != null) {                if (next == null){                    next = (parent.left != null) ? parent.left: parent.right;                }                if (parent.left != null){                    if (pre != null) {                        pre.next = parent.left;                        pre = pre.next;                    } else {                        pre = parent.left;                    }                }                if (parent.right != null) {                    if (pre != null) {                        pre.next = parent.right;                        pre = pre.next;                    } else {                        pre = parent.right;                    }                }                parent = parent.next;            }            parent = next;        }    }}


0 0
原创粉丝点击