leetcode 116. Populating Next Right Pointers in Each Node
来源:互联网 发布:php进阶书籍推荐知乎 编辑:程序博客网 时间:2024/05/18 01:41
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 to NULL.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 7After calling your function, the tree should look like: 1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
核心一点,如果有孩子
root.left.next = root.right;if(root.next!=null) root.right.next = root.next.left;
很简单的写法
/** * 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){ TreeLinkNode p = root.next; root.left.next = root.right; if(p!=null){ root.right.next = p.left; } connect(root.left); connect(root.right); } }}
阅读全文
0 0
- LeetCode 116. Populating Next Right Pointers in Each Node
- [LeetCode]116.Populating Next Right Pointers in Each Node
- [Leetcode] 116. Populating Next Right Pointers in Each Node
- LeetCode 116. Populating Next Right Pointers in Each Node
- [leetcode] 116.Populating Next Right Pointers in Each Node
- 116. Populating Next Right Pointers in Each Node LeetCode
- leetcode 116. Populating Next Right Pointers in Each Node
- Leetcode 116. Populating Next Right Pointers in Each Node
- LeetCode *** 116. Populating Next Right Pointers in Each Node
- leetcode-116. Populating Next Right Pointers in Each Node
- LeetCode 116. Populating Next Right Pointers in Each Node
- [LeetCode]problem 116. Populating Next Right Pointers in Each Node
- Leetcode 116. Populating Next Right Pointers in Each Node
- LeetCode 116. Populating Next Right Pointers in Each Node
- [leetcode] 116. Populating Next Right Pointers in Each Node
- leetcode-java-116. Populating Next Right Pointers in Each Node
- [LeetCode] 116. Populating Next Right Pointers in Each Node
- 【Leetcode】116. Populating Next Right Pointers in Each Node
- 【BaseRVFragment】基于MVP+Dagger2+Retrofit+Rxjava框架之BaseRVFragment
- 教你写响应式框架(一)
- 老韩复习!!课程!!
- CentOS7下 安装mysql
- npm install失败解决办法
- leetcode 116. Populating Next Right Pointers in Each Node
- 算法谜题96 平铺楼梯区域
- IOS-- UIView中的坐标转换convertPoint
- maven项目部署后没有依赖的jar包
- C#中Math类集合
- 数据结构(栈--两个队列实现)
- [BZOJ4816][Sdoi2017]数字表格 数学
- numpy知识
- android柱状图实现