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 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
题意:对于一个完美二叉树,将所有节点的next指向其同层右边的节点,每一层的最后一个节点,指向null
分类:二叉树
解法1:层次遍历,使用两个栈,stack1存储当前层,stack2存储下一层,对于stack1,每次pop(),将左右子树添加到stack2,同时pre指向前一个节点,连接pre和当前元素,直到stack1为空,此时stack2装满下一层元素,将stack2元素复制到stack1,清空stack2,重复上述过程。
本质就是层次遍历,特点是使用了两个栈。
-
-
-
-
-
-
-
-
- public class Solution {
- public void connect(TreeLinkNode root) {
- if(root==null) return;
- Stack<TreeLinkNode> stack1 = new Stack<TreeLinkNode>();
- List<TreeLinkNode> stack2 = new ArrayList<TreeLinkNode>();
- stack1.add(root);
- while(stack1.size()>0||stack2.size()>0){
- TreeLinkNode pre = null;
- while(stack1.size()>0){
- TreeLinkNode t = stack1.pop();
- if(t.left!=null) stack2.add(t.left);
- if(t.right!=null) stack2.add(t.right);
- if(pre!=null){
- pre.next = t;
- }
- pre = t;
- }
- Collections.reverse(stack2);
- stack1.addAll(stack2);
- stack2.clear();
- }
- }
- }
解法2:递归解决。创建一个能够连接相邻节点,以及这个两个节点的所有子节点的函数。
-
-
-
-
-
-
-
-
- public class Solution {
- public void connect(TreeLinkNode root) {
- if(root==null) return;
- helper(root,null);
- }
-
-
-
-
-
- public void helper(TreeLinkNode root,TreeLinkNode next){
- root.next = next;
- if(root.left!=null&&root.right!=null){
- helper(root.left,root.right);
- if(next!=null){
- helper(root.right,next.left);
- }else{
- helper(root.right,null);
- }
- }
- }
- }
解法3,:该题的思路在于,要连接一个节点的右节点,和它相邻节点的左节点,所有我们扫描一个节点的时候,要知道其相邻节点。上面的递归方法,使用了参数的形式来记录相邻节点。在层次遍历中,我们手动记录相邻节点即可。另外,还要记录每层的第一个节点,从而省去队列。
-
-
-
-
-
-
-
-
- public class Solution {
- public void connect(TreeLinkNode root) {
- if(root==null) return;
- TreeLinkNode cur = root;
- while(root.left!=null){
- cur = root;
- while(cur!=null){
- cur.left.next = cur.right;
- if(cur.next!=null) cur.right.next = cur.next.left;
- cur = cur.next;
- }
- root = root.left;
- }
- }
- }
解法4:思路和解法3一样
-
-
-
-
-
-
-
-
- public class Solution {
- public void connect(TreeLinkNode root) {
- if(root==null) return;
- TreeLinkNode cur = root;
- TreeLinkNode next = null;
- TreeLinkNode level = root.left;
- cur.next = next;
- while(cur!=null){
- if(cur.left!=null&&cur.right!=null){
- cur.left.next = cur.right;
- if(next!=null){
- cur.right.next = next.left;
- }else{
- cur.right.next = null;
- }
- }
- if(next!=null){
- cur = next;
- next = next.next;
- }else{
- cur = level;
- if(cur!=null){
- level = cur.left;
- next = cur.next;
- }
- }
- }
- }
- }
原文链接http://blog.youkuaiyun.com/crazy__chen/article/details/46494313