Leetcode-117: Populating Next Right Pointers in Each Node II
来源:互联网 发布:阿里云yum安装mysql 编辑:程序博客网 时间:2024/06/04 18:06
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
/** * 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; TreeLinkNode head = root, cur = null; while (head != null) { cur = head; boolean nextLevel = false; while (cur != null) { TreeLinkNode childNext = null; if (!nextLevel && (cur.left != null || cur.right != null)) { nextLevel = true; head = cur.left == null ? cur.right : cur.left; } TreeLinkNode nextParent = cur.next; while (nextParent != null && nextParent.next != null && (nextParent.left == null && nextParent.right == null)){ nextParent = nextParent.next; } if (nextParent != null) { childNext = nextParent.left == null ? nextParent.right : nextParent.left; } if (cur.right != null) { if (cur.left != null) cur.left.next = cur.right; cur.right.next = childNext; } else if (cur.left != null) cur.left.next = childNext; cur = nextParent; } if (!nextLevel) break; } }}
阅读全文
0 0
- LeetCode 117 Populating Next Right Pointers in Each Node II
- LeetCode: Populating Next Right Pointers in Each Node II [117]
- LeetCode 117 Populating Next Right Pointers in Each Node II
- [leetcode 117] Populating Next Right Pointers in Each Node II
- [LeetCode 117]Populating Next Right Pointers in Each Node II
- leetcode || 117、Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II - LeetCode 117
- leetcode[117]:Populating Next Right Pointers in Each Node II
- Leetcode #117 Populating Next Right Pointers in Each Node II
- LeetCode 117: Populating Next Right Pointers in Each Node II
- LeetCode 117:Populating Next Right Pointers in Each Node II
- leetcode 117 Populating Next Right Pointers in Each Node II
- Leetcode 117 Populating Next Right Pointers in Each Node II
- LeetCode 117 Populating Next Right Pointers in Each Node II
- Leetcode-117: Populating Next Right Pointers in Each Node II
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node II
- git提交到github远程分支
- 骨牌覆盖
- 设计模式_抽象工厂模式(4)
- 结构体中运算符的重载
- setuptools 和easy_install介绍和安装方法
- Leetcode-117: Populating Next Right Pointers in Each Node II
- LuoguP1637 三元上升子序列
- HDU 1950(LIS)
- syntax error: non-declaration statement outside function body
- 关于Java中的 i = i ++ 的问题
- 聊天室
- [编程题] 头条校招
- HDU4358【离散化+DFS序+莫队算法】
- AI开发实战3-定制自己的Screen