Populating Next Right Pointers in Each Node II 任意(非完美)二叉树添加next指针 @LeetCode
来源:互联网 发布:linux 播放 4k 视频 编辑:程序博客网 时间:2024/06/06 03:35
http://blog.csdn.net/fightforyourdream/article/details/14514165
在上题基础上扩展,此时数不是完美二叉树,而是任意二叉树
这道题有些tricky,有时间回头再研究一下
package Level4;import Utility.TreeLinkNode;/** * Populating Next Right Pointers in Each Node II * * 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 -> NULLDiscuss * */public class S117 {public static void main(String[] args) {}public static void connect(TreeLinkNode root) {// 空节点就直接返回if (root == null){return;}// 找到与root同一行的next nodeTreeLinkNode rootNext = root.next;TreeLinkNode next = null;// 下一个被连接的对象// rootNext如果是null说明已经处理完这一层的所有node// next不等于null说明找到了找到最左边的下一个被连接的对象while (rootNext != null && next == null){if (rootNext.left != null){// 优先找左边next = rootNext.left;} else{next = rootNext.right;}rootNext = rootNext.next;} if (root.left != null){if (root.right != null){//内部相连root.left.next = root.right;}else{// 跨树相连root.left.next = next;}}if (root.right != null){// 跨树相连root.right.next = next;}connect(root.right);// 要先让右边都先连起来connect(root.left); }}
第二次做修改了一个地方的bug(在找rootnext时增加了break,及时退出),居然提交两次就通过了。
果然做LeetCode的真谛在于:遇到不懂 => 查答案 => 自己理解写出来或背下来 => 过一段时间后第二遍重做
比如这道题,我印象最深刻的是一定要先处理右子树再处理左子树,知道这点就足够写出来了。
关键就是找到next节点是在哪里,而这又要求找到rootnext节点在哪里。那个while循环就是破题的关键。
/** * 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 rootnext = root.next; TreeLinkNode next = null; while(rootnext != null){ if(rootnext.left != null){ next = rootnext.left; break; }else if(rootnext.right != null){ next = rootnext.right; break; }else{ rootnext = rootnext.next; } } if(root.right != null){ root.right.next = next; } if(root.left != null){ if(root.right != null){ root.left.next = root.right; }else{ root.left.next = next; } } connect(root.right); connect(root.left); }}
- Populating Next Right Pointers in Each Node II 任意(非完美)二叉树添加next指针 @LeetCode
- Populating Next Right Pointers in Each Node 完美二叉树添加next指针 @LeetCode
- Leetcode 117 Populating Next Right Pointers in Each Node II 二叉树填充next指针指向右侧结点 II
- 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 二叉树Next指针
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- [C++]LeetCode: 124 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 II
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- leetcode 79: 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
- 【leetcode】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
- Redis应用场景
- 带传动产生松边和紧边的原因
- Search in Rotated Sorted Array 旋转数组里查找数 @LeetCode
- Binary Tree Level Order Traversal II 二叉树按层遍历(反向输出)@LeetCode
- petsc 矩阵测试
- Populating Next Right Pointers in Each Node II 任意(非完美)二叉树添加next指针 @LeetCode
- Apache HttpClient 4.3开发指南
- bash脚本文件注释
- qsort函数用法
- Django 学习笔记(六)
- hdoj1421 搬寝室 (dp)
- 浏览器内核比较
- [转] VIM解决中文乱码
- ENGLISH资料收集(12)-古代经典著作书名