Leetcode: Populating Next Right Pointers in Each Node
来源:互联网 发布:淘宝借呗客服电话 编辑:程序博客网 时间:2024/05/22 13:21
题目:
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 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ / \4->5->6->7 -> NULL
思路分析:
我们首先利用队列queue层次遍历二叉树将结果放入vector中,因为题意假设是完全二叉树,所以二叉树的每层都是2i个节点。利用这个规律我们可以得到每一层的节点,然后循环的从每一层第一个节点指向右边后一个节点。
C++参考代码:
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} * }; */class Solution{public: void connect(TreeLinkNode *root) { if (!root) return; queue<TreeLinkNode*> nodeQueue; vector<TreeLinkNode*> nodeVector;//二叉树层次遍历结果 nodeQueue.push(root); TreeLinkNode *treeNode = nullptr; //二叉树层次遍历 while (!nodeQueue.empty()) { treeNode = nodeQueue.front(); nodeQueue.pop(); nodeVector.push_back(treeNode); if (treeNode->left) nodeQueue.push(treeNode->left); if (treeNode->right) nodeQueue.push(treeNode->right); } vector<TreeLinkNode*>::size_type size = nodeVector.size(); int count = 0;//遍历过程中节点指针 int level = 0;//树的层 int step = 0;//记录满二叉树节点的个数 while (count < size) { step += int(pow(2, level)); while ((count < step - 1) && (count < size)) { nodeVector[count]->next = nodeVector[++count]; } count++; level++; } }};
Java参考代码:
/** * 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; Queue<TreeLinkNode> nodeQueue = new LinkedList<TreeLinkNode>(); List<TreeLinkNode> nodeList = new ArrayList<TreeLinkNode>(); TreeLinkNode treeNode = null; nodeQueue.offer(root); //层次遍历二叉树,结果放入nodeList中 while (!nodeQueue.isEmpty()) { treeNode = nodeQueue.poll(); nodeList.add(treeNode); if (treeNode.left != null) nodeQueue.offer(treeNode.left); if (treeNode.right != null) nodeQueue.offer(treeNode.right); } int size = nodeList.size(); int count = 0; int level = 0; int step = 0; while (count < size) { step += Math.pow(2, level); while ((count < step - 1) && (count < size)) { nodeList.get(count).next = nodeList.get(++count); } ++count; ++level; } }}
0 0
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- leetcode Populating Next Right Pointers in Each Node &&Populating Next Right Pointers in Each Node||
- LeetCode Populating Next Right Pointers in Each Node & Populating Next Right Pointers in Each Node I
- 【Leetcode】Populating Next Right Pointers in Each Node in JAVA
- LeetCode Populating Next Right Pointers in Each Node
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node
- LeetCode: Populating Next Right Pointers in Each Node
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node
- [LeetCode] Populating Next Right Pointers in Each Node II
- 【leetcode】 Populating Next Right Pointers in Each Node
- leetcode 78: Populating Next Right Pointers in Each Node
- leetcode 79: Populating Next Right Pointers in Each Node II
- Leetcode: Populating Next Right Pointers in Each Node
- [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
- php 二维数组排序算法函数
- 配置vim Python IDE 开发环境
- [Leetcode] 107. Binary Tree Level Order Traversal II
- android客户端和java服务端之间用socket来传输图片(示例代码)
- Ubuntu +cuda+theano 解决了一个问题
- Leetcode: Populating Next Right Pointers in Each Node
- libc++abi.dylib handler threw exception
- 蓝桥杯 提取子串
- 9个优秀网上免费标签云生成工具
- Linux内核分析:分析system_call中断处理过程
- Java几款性能分析工具的对比
- Ubuntu 14.04 Eclipse提示框背景为黑色问题完全解决方案
- LinkedList-二叉树遍历
- Java编程提高性能时需注意的地方