Leetcode: Populating Next Right Pointers in Each Node II
来源:互联网 发布:淘宝修改折扣价权重 编辑:程序博客网 时间:2024/05/19 07:07
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
按层次遍历可以继续用,不过不是constant extra space.
/** * 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; TreeLinkNode *cur = NULL; queue<TreeLinkNode*> trees; trees.push(root); trees.push(NULL); while (!trees.empty()) { cur = trees.front(); trees.pop(); if (cur != NULL) { cur->next = trees.front(); if (cur->left != NULL) { trees.push(cur->left); } if (cur->right != NULL) { trees.push(cur->right); } } else if (!trees.empty()) { trees.push(NULL); } } }};
对应I的第二方法:
class Solution {public: void connect(TreeLinkNode *root) { if (root == NULL || root->left == NULL && root->right == NULL) { return; } connect(root->left); connect(root->right); int level = 0; while (true) { TreeLinkNode *most_right = mostRight(root->left, level); TreeLinkNode *most_left = mostLeft(root->right, level); if (most_right != NULL && most_left != NULL) { most_right->next = most_left; ++level; } else { break; } } } TreeLinkNode *mostRight(TreeLinkNode* root, int level) { if (root == NULL) { return NULL; } else if (level == 0) { return root; } TreeLinkNode *right = mostRight(root->right, level - 1); if (right == NULL) { right = mostRight(root->left, level - 1); } return right; } TreeLinkNode *mostLeft(TreeLinkNode* root, int level) { if (root == NULL) { return NULL; } else if (level == 0) { return root; } TreeLinkNode *left = mostLeft(root->left, level - 1); if (left == NULL) { left = mostLeft(root->right, level - 1); } return left; }};对应I的第三种方法,基本是从http://blog.csdn.net/fightforyourdream/article/details/16854731 借鉴而来。学习了。
class Solution {public: void connect(TreeLinkNode *root) { if (root == NULL || root->left == NULL && root->right == NULL) { return; } TreeLinkNode *rnext = root->next; TreeLinkNode *next = NULL; while (rnext != NULL && next == NULL) { if (rnext->left != NULL) { next = rnext->left; } else if (rnext->right != NULL) { next = rnext->right; } rnext = rnext->next; } if (root->left != NULL) { if (root->right != NULL) { root->left->next = root->right; } else { root->left->next = next; } } if (root->right != NULL) { root->right->next = next; } connect(root->right); connect(root->left); }};
===================第二次=================
/** * 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 || root->left == NULL && root->right == NULL) { return; } TreeLinkNode* childNext = NULL; TreeLinkNode* next = root->next; while (childNext == NULL && next != NULL) { if (next->left != NULL) { childNext = next->left; } else if (next->right != NULL) { childNext = next->right; } next = next->next; } if (root->left != NULL) { root->left->next = root->right ? root->right : childNext; } if (root->right != NULL) { root->right->next = childNext; } connect(root->right); connect(root->left); }};
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 II
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- leetcode 79: 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 II
- 【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 II
- 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 II
- 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 II
- 【LeetCode OJ】Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- 查看自己端口被谁占用的几个命令(以9000为例)
- 《Java并发》:第三节 创建有返回值的线程
- 《交互设计之路》读书笔记
- 修改gp集群主机名称
- 乌班图系统Ubuntu 12.04安装教程(图文详解)
- Leetcode: Populating Next Right Pointers in Each Node II
- linux-od
- SCANF返回值问题
- vxworks的堆和栈
- 枸杞子
- 母牛的故事
- android 动态加载自定义控件
- 系统启动过程简介
- User's $HOME/.dmrc file is being ignored解决办法终结版