Add Sibling to Binary Tree
来源:互联网 发布:淘宝网卖家客户端 编辑:程序博客网 时间:2024/05/16 16:09
Question:
Given a binary tree
struct Node {
Node* leftChild;
Node* rightChild;
Node* nextRight;
}
Populate the nextRight pointers in each node.
The first idea come out should be the BFS tree algorithm, which can traverse the binary tree layer by layer. We only need to connect the sibling for each layer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<pre>
int
populateNextRightBFS(Node* root) {
if
(!root)
return
0;
queue <Node*> q;
q.push(root);
int
nextLayer = 0;
int
curLayer = 1;
Node* prev = NULL;
while
(!queue.empty()) {
Node* cur = queue.front();
if
(prev)
prev->sibling = cur;
if
(cur->left) {
queue.push(cur->left);
nextLayer++;
}
if
(cur->right) {
queue.push(cur->right);
nextLayer++;
}
queue.pop();
curLayer--;
prev = cur;
if
(!curLayer) {
curLayer = nextLayer;
nextLayer = 0;
prev = NULL;
}
}
return
0;
}
Above algorithm uses an queue, which can be saved actually. We could use the parent’s sibling to visit children’ siblings. Then we have the following algorithm.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<pre>
int
populateNextRightExt(Node* root) {
if
(!root)
return
0;
if
(root->left)
root->left->sibling= root->right;
Node* cur = root;
if
(root->right) {
while
(cur->sibling && !cur->sibling->left && !cur->sibling->right)
cur = cur->sibling;
if
(cur->sibling && cur->sibling->left)
root->right->sibling = cur->sibling->left;
else
if
(cur->sibling && cur->sibling->right)
root->right->sibling = cur->sibling->left;
else
root->right->sibling = NULL;
}
populateNextRight(root->right);
populateNextRight(root->left);
return
0;
}
P.S If the sibling only points to the node in one-step right, and NULL otherwise, we do not need to search for its sibling far away. The simplified algorithm is as follows.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<pre>
int
populateNextRight(Node* root) {
if
(!root)
return
0;
if
(root->left)
root->left->sibling= root->right;
if
(root->right)
root->right->sibling = (root->sibling) ? root->sibling->left : NULL;
populateNextRight(root->right);
populateNextRight(root->left);
return
0;
}
0 0
- Add Sibling to Binary Tree
- Medium: Binary Tree Paths Add to List
- Add to List 563. Binary Tree Tilt
- Add to List 257. Binary Tree Paths
- leetcode104~Maximum Depth of Binary Tree Add to List
- 104.Maximum Depth of Binary Tree Add to List
- leetcode124~Binary Tree Maximum Path Sum Add to List
- LeetCode98. Validate Binary Search Tree Add to List
- 104. Maximum Depth of Binary Tree Add to List DescriptionHintsSubmissions
- LeetCode 98. Validate Binary Search Tree Add to List
- LeetCode 99. Recover Binary Search Tree Add to List
- Add to List 111. Minimum Depth of Binary Tree
- 【LeetCode】Add to List 606. Construct String from Binary Tree
- leetcode- Add to List 669. Trim a Binary Search Tree
- binary tree path add
- leetcode 108. Convert Sorted Array to Binary Search Tree Add to List
- leetcode_ Add to List 108. Convert Sorted Array to Binary Search Tree
- Add to List 67. Add Binary
- DUP_VAL_ON_INDEX 作用和用法
- 缓存文件
- 干完了选择咱们再来干一干循环~
- Qt---自定义菜单
- mysql权限管理(2014-09-15)
- Add Sibling to Binary Tree
- php编码问题
- Qt Creator 3.1.2+Qt 5.3.1+OpenCV 3.0 开发环境下,找不到头文件的错误解决办法
- 不支持 URI 格式。
- 如何从技术型创始人转型成企业领导人?
- 运放的噪声增益
- Sqoop工具使用(一)--从oracle导入数据到hive
- WEB服务器与应用服务器的区别
- 【WEB基础】⑤一些前端小东西