题目:
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
解答:
public class Solution { public void connect(TreeLinkNode root) { //重点是记录current, head, prev这三个结点 TreeLinkNode curt = root; TreeLinkNode head = null; TreeLinkNode prev = null; while (curt != null) { while (curt != null) { if (curt.left != null) { if (prev != null) { prev.next = curt.left; } else { head = curt.left; } prev = curt.left; } if (curt.right != null) { if (prev != null) { prev.next = curt.right; } else { head = curt.right; } prev = curt.right; } curt = curt.next; } curt = head; head = null; prev = null; } } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/64883.html
摘要:题目要求给一个完全二叉树,将当前节点的值指向其右边的节点。而在中则是提供了一个不完全的二叉树,其它需求没有发生改变。我们需要使用一个变量来存储父节点,再使用一个变量来存储当前操作行的,将前一个指针指向当前父节点的子节点。 题目要求 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; ...
摘要:原题链接广度优先搜索复杂度时间空间思路相当于是遍历二叉树。代码记录本层节点的个数最后一个节点的是,不做处理递归法复杂度时间空间递归栈空间思路由于父节点多了指针,我们不用记录父节点的父节点就能直到它相邻的节点。 Populating Next Right Pointers in Each Node I Given a binary tree struct TreeLinkNode { ...
摘要:复杂度思路设置一个指向下一层要遍历的节点的开头的位置。 LeetCode[117] Population Next Right Pointers in Each Node II 1 / 2 3 / 4 5 7 After calling your function, the tree should look like: ...
Problem Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Lets take the foll...
摘要:将操作记录为一列。在列表上进行操作被称为光栅化。能够运行产生位图的命令加速光栅化注意到此时像素并不在屏幕上。因此,线程将划分为。根据与的距离确定的优先级。被包装在一个中,该对象被提交给浏览器进程。 This talk is about how Chrome turns web content into pixels. The entire process is called rend...
阅读 1769·2021-09-03 10:50
阅读 1313·2019-08-30 15:55
阅读 3350·2019-08-30 15:52
阅读 1206·2019-08-30 15:44
阅读 899·2019-08-30 15:44
阅读 3286·2019-08-30 14:23
阅读 3534·2019-08-28 17:51
阅读 2268·2019-08-26 13:52