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; } if (root.left != null) { if(root.left.next != null) { return; } if (root.right != null) { root.left.next = root.right; } else { findNextAndConnect(root.next, root.left); } } if (root.right != null) { if (root.right.next != null){ return; } findNextAndConnect(root.next, root.right); } connect(root.next); connect(root.left); connect(root.right); } public void findNextAndConnect(TreeLinkNode node, TreeLinkNode toLink){ for (; node != null; node = node.next) { if (node.left != null) { toLink.next = node.left; break; } else if (node.right != null) { toLink.next = node.right; break; } } } }
相关推荐
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给定一个二叉树填充
lru cache leetcode leetcode 记录自己刷leetcode时遇到的一些值得记下来的题目, 分为一些子项 bytedance ...populating-next-right-pointers-in-each-node sum-root-to-leaf-numbers best-time-to-buy
四平方和定理 leetcode Leetcode practice Table of content Tree 92.reverse-linked-list-ii (反转链表 II) 94.binary-tree-in...116.populating-next-right-pointers-in-each-node (填充每个节点的下一个右侧节点
蓄水池算法 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...
What's New in 13.2.5 (VCL Product Line) New Major Features in 13.2 What's New in VCL Products 13.2 Feature Highlights To learn about feature highlights in this version, please refer to the following...
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 ...
LiveLinq is a class library that augments the functionality of LINQ in two related directions: It makes LINQ faster LiveLinq uses indexing and other optimizations to speed up LINQ queries in memory....