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
既然题目非要说完全二叉树,那就按照完全来做
/** * 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 || root.left == null) { return; } root.left.next = root.right; if (root.next == null) { root.right.next = null; } else { root.right.next = root.next.left; } connect(root.left); connect(root.right); } }
相关推荐
Populating Next Right Pointers in Each Node II 二叉树的构建 Construct Binary Tree from Preorder and Inorder Traversal Construct Binary Tree from Inorder and Postorder Traversal 二叉查找树 Unique ...
Populating Next Right Pointers in Each Node I && II) PIE (未录入) CC150 (未录入) EPI (未录入) 每一个题库对应problems路径下的一个文件夹,每一个题目对应相应题库下的一个Python文件。每一个题目都要至少实现...
leetcode卡 leetcode_python ...Populating Next Right Pointers in Each Node 2020-01-29 783 Minimum Distance between bst nodes 2020-01-30 173 二叉搜索树迭代器 2020-01-31 230 二叉搜索树中
lru缓存leetcode leetcode 大批 41. First Missing Positive 广度优先搜索 773. Sliding Puzzle 864. Shortest Path to Get All Keys 深度优先搜索 ...in ...Populating Next Right Pointers in Each No
[117]填充每个节点的下一个右侧节点指针 II|populating-next-right-pointers-in-each-node-ii给定一个二叉树填充
四平方和定理 leetcode Leetcode practice Table of content Tree 92.reverse-linked-list-ii (反转链表 ...94.binary-tree-inorder-...116.populating-next-right-pointers-in-each-node (填充每个节点的下一个右侧节点
lru cache leetcode leetcode 记录自己刷leetcode时遇到的一些值得记下来的题目, 分为一些子项 bytedance ...populating-next-right-pointers-in-each-node sum-root-to-leaf-numbers best-time-to-buy
蓄水池算法 leetcode leetcode Post: 《双指针的魅力》 《常见面试题思想方法整理》 ...populating-next-right-pointers-in-each-node-ii: 二级指针代码虽然简洁优雅,但是对性能有影响,不如一级指针加if else判断快。
leetcode 跳跃 Algorithm 算法题解: 包括书籍算法, 程序员算法面试指南, 还有leetcode算法题 运行代码 cd **/**/ ...binary-tree-inorder-traversal ...construct-binary-tree-from-...populating-next-right-pointers-in-eac
Populating a Generic Data List with Generic method。
Populating a Linked Data Entity Name System A Big Data Solution to Unsupervised Instance Matching 英文无水印原版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 ...
php解决Deprecated Automatically populating is deprecated and will be removed
U盘安装CentOS,镜像制作工具。使用其它工具,无法正确引导安装的话。请使用此工具。
13.2.1. Populating the cache 13.2.2. Removing cache entries 13.3. Declaring caching in XML 13.4. Summary Chapter 14. Securing methods 14.1. Securing methods with annotations 14.1.1. Restricting method...
Normally, populating a junction table requires use of a Main-Form/Sub-Form interface with the junction table being populated by a Combo Box which is in the Sub-Form. (Don't know what I am talking...
•Q583680 - Skin Editor - Changes to the Borders.Left, Borders.Top, and/or Borders.Right property sets are not stored to a skin project if these are the only changes made to a skin element •Q573512 -...
So in the next version I will remove the IPAddress property. Thank you, Roger.1.8.0- Well, it seems that I made a mistake, I investigated the previous behavior and it is a fault of the SMTP (RFC 821)...
Chapter 3—Creating,Populating,Altering,and Deleting Tables 71 Chapter 4—Joins 101 Chapter 5—Functions 127 Chapter 6—Query Development and Derived Structures 179 Chapter 7—Set Operations 213 ...
Live view is a LINQ query result that is kept constantly up-to-date without re-populating it every time its base data changes. This makes LiveLinq extremely useful in common data-binding scenarios ...