Populating Next Right Pointers in Each Node--为每一个节点填充next right指针
2014-01-28 09:25
561 查看
原题:
Given a binary tree
=>给定一个二叉树
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
=>把每一个节点的next指针指向它右面的节点,假如右边没有节点,则指向null
Initially, all next pointers are set to
=>所有的next节点都是初始化为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).
=>假设是一个完美的二叉树(就是所有的叶子都是在一个level上,所有的父子树都有两个子节点)
For example,
=>例如
Given the following perfect binary tree,
=>给定的完美二叉树
After calling your function, the tree should look like:
=>在执行了函数之后,就如下图所示:
[/code]
晓东分析:
首先想到的就是二叉树的遍历,只要把先遍历到的左子树指向右子树,然后就可以组成如下的三角形,其中null是初始化的值。(5也是指向null的,未画出)
1 -> null
/ \
2 -> 3 ->null
/ \ / \
4->5 6->7->null
下面一步,就是把5和6之间联系起来,这就是右子树的next指向自己next的左子树。这样就可以得到
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
下面就是各种遍历的算法选择了。选择了一个先序遍历,见/article/1622886.html里面有详细描述。于是有了下面的代码
代码实现:
实现一:递归
运行结果:
实现二:
非递归实现:
运行结果:
令人奇怪的是非递归反而比递归的算法慢了,比较奇怪。
若您有更好的算法可以提出,谢谢
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.
=>把每一个节点的next指针指向它右面的节点,假如右边没有节点,则指向null
Initially, all next pointers are set to
NULL.
=>所有的next节点都是初始化为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).
=>假设是一个完美的二叉树(就是所有的叶子都是在一个level上,所有的父子树都有两个子节点)
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. * 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) { } };
[/code]
晓东分析:
首先想到的就是二叉树的遍历,只要把先遍历到的左子树指向右子树,然后就可以组成如下的三角形,其中null是初始化的值。(5也是指向null的,未画出)
1 -> null
/ \
2 -> 3 ->null
/ \ / \
4->5 6->7->null
下面一步,就是把5和6之间联系起来,这就是右子树的next指向自己next的左子树。这样就可以得到
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
下面就是各种遍历的算法选择了。选择了一个先序遍历,见/article/1622886.html里面有详细描述。于是有了下面的代码
代码实现:
实现一:递归
/** * 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 (NULL == root) { return; } TreeLinkNode *left = root->left; TreeLinkNode *right = root->right; /* set the next pointer for his left child */ if (NULL != left) { left->next = right; } /* set the next pointer for his right child */ if (NULL != right && NULL != root->next) { right->next = root->next->left; } /* do it recursively */ connect(left); connect(right); } };
运行结果:
14 / 14 test cases passed. | Status:Accepted |
Runtime: 52 ms |
非递归实现:
/** * 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 == NULL) return; stack<TreeLinkNode*> TreeStack; while(root || !TreeStack.empty()){ while(root){ TreeStack.push(root); if(root->left){ root->left->next = root->right; if(root->next) root->right->next = root->next->left; } root = root->left; } root = TreeStack.top(); TreeStack.pop(); root = root->right; } } };
运行结果:
14 / 14 test cases passed. | Status:Accepted |
Runtime: 104 ms |
若您有更好的算法可以提出,谢谢
相关文章推荐
- [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)
- [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)
- leetCode 116.Populating Next Right Pointers in Each Node (为节点填充右指针) 解题思路和方法
- LeetCode OJ:Populating Next Right Pointers in Each Node II(指出每一个节点的下一个右侧节点II)
- 【二叉树的递归】06填充每个节点中的下一个正确的指针【Populating Next Right Pointers in Each Node】
- LeetCode OJ:Populating Next Right Pointers in Each Node(指出每一个节点的下一个右侧节点)
- (Java) LeetCode 116. Populating Next Right Pointers in Each Node —— 填充同一层的兄弟节点
- populating-next-right-pointers-in-each-node(填充每个节点中右下角的指针)
- leetCode 116.Populating Next Right Pointers in Each Node (为节点填充右指针) 解题思路和方法
- [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针
- leetcode:Populating Next Right Pointers in Each Node(常数空间,连接二叉树每一层所有节点)【面试算法题】
- Leetcode 116 Populating Next Right Pointers in Each Node 二叉树填充next指针指向右侧结点
- [Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针
- leetcode:Populating Next Right Pointers in Each Node II (顺序连接二叉树每一层节点)【面试算法题】
- LeetCode-Populating Next Right Pointers in Each Node-填充结点的右指针-二叉树递归
- LeetCode OJ 之 Populating Next Right Pointers in Each Node (为每个结点填充右指针)
- Populating Next Right Pointers in Each Node 设置二叉树的next节点
- [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二
- [LeetCode] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针
- LeetCode-116. Populating Next Right Pointers in Each Node【二叉树同层节点构成链表】