[LeetCode 117]Populating Next Right Pointers in Each Node II
来源:互联网 发布:数据分析师好找工作吗 编辑:程序博客网 时间:2024/05/18 01:09
题目链接:populating-next-right-pointers-in-each-node-ii
相同题型:Populating Next Right Pointers in Each Node
import java.util.LinkedList;/** * 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 7After calling your function, the tree should look like: 1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL * */public class PopulatingNextRightPointersInEachNodeII {public class TreeLinkNode {int val;TreeLinkNode left, right, next;TreeLinkNode(int x) {val = x;}}//解法一 递归法//61 / 61 test cases passed.//Status: Accepted//Runtime: 290 ms//Submitted: 0 minutes ago//时间复杂度:O(n) 空间复杂度:O(1) public void connect(TreeLinkNode root) {if (root != null) {TreeLinkNode firstNode = null; //下一层的首个节点TreeLinkNode nextNode = null; //下一层已链接的尾节点while (root != null) {if (root.left != null) {if (firstNode == null) {firstNode = root.left;nextNode = firstNode;} else {nextNode.next = root.left;nextNode = nextNode.next;}}if (root.right != null) {if (firstNode == null) {firstNode = root.right;nextNode = firstNode;} else {nextNode.next = root.right;nextNode = nextNode.next;}}root = root.next;}connect(firstNode);} } //解法二 迭代法// 61 / 61 test cases passed.// Status: Accepted// Runtime: 271 ms// Submitted: 0 minutes ago //时间复杂度:O(n) 空间复杂度:O(1) public void connect1(TreeLinkNode root) {while (root != null) {TreeLinkNode firstNode = null; //下一层的首个节点TreeLinkNode nextNode = null; //下一层已链接的尾节点while (root != null) {if (root.left != null) {if (firstNode == null) {firstNode = root.left;nextNode = firstNode;} else {nextNode.next = root.left;nextNode = nextNode.next;}}if (root.right != null) {if (firstNode == null) {firstNode = root.right;nextNode = firstNode;} else {nextNode.next = root.right;nextNode = nextNode.next;}}root = root.next;}root = firstNode;} } // 解法三:层次遍历法// 61 / 61 test cases passed.// Status: Accepted// Runtime: 359 ms// Submitted: 0 minutes ago //时间复杂度:O(n) 空间复杂度:O(n) public void bfs(TreeLinkNode root) { if(root == null) { return; } LinkedList<TreeLinkNode> stack = new LinkedList<TreeLinkNode>(); stack.add(root); while(!stack.isEmpty()) { int size = stack.size(); for (int i = 0; i < size; i++) {TreeLinkNode node = stack.remove(0);node.next = (i == size - 1) ? null : stack.getFirst();if(node.left != null)stack.add(node);if(node.right != null)stack.add(node.right);} } } public static void main(String[] args) {// TODO Auto-generated method stub}}
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
- chorme浏览器图标没见了,解决办法
- Android项目打第三方jar包
- 仙林鼎山游乐园(有向图判断环)
- 找出数组中两个只出现一次的数字
- 264B (DP)
- [LeetCode 117]Populating Next Right Pointers in Each Node II
- HDU4282 A very hard mathematic problem(枚举+二分)
- hexo创建blog
- MyEclipse使用经验总结
- hsqldb的使用
- Oracle监听配置 错误12541
- ThinkPHP 解析二维码
- android processBar seekBar 定制(底部间隙问题)
- iOS 7 的多任务