leetcode:Populating Next Right Pointers in Each Node II (顺序连接二叉树每一层节点)【面试算法题】
来源:互联网 发布:php权限管理设计思路 编辑:程序博客网 时间:2024/05/21 08:36
题目:
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
题意把二叉树每一层所有的节点顺序链接,只能开辟常数空间。
这题与上道题不同点在于二叉树不是完全二叉树,因此结构差别较大,不能用上道题的方法。
如果当前节点有左右子节点,先把他们链接起来,因为肯定是相邻的。
然后通过root的next指针,找到需要与当前节点子节点连接的后继节点。
因为涉及到root的next指针查询,递归需要先处理完右子树,以便左子树节点next指针的连接。
/** * 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; TreeLinkNode *temp=root->next,*p; if(root->left &&root->right) { p=root->right; root->left->next = root->right; } else if(root->right)p=root->right; else if(root->left)p=root->left; else p=NULL; if(p) { while(temp) { if(temp->left) { p->next = temp->left; break; } else if(temp->right) { p->next = temp->right; break; } else temp=temp->next; } } connect(root->right); connect(root->left); }};// http://blog.csdn.net/havenoidea
题解目录
- leetcode:Populating Next Right Pointers in Each Node II (顺序连接二叉树每一层节点)【面试算法题】
- leetcode:Populating Next Right Pointers in Each Node(常数空间,连接二叉树每一层所有节点)【面试算法题】
- Populating Next Right Pointers in Each Node II 顺序链接二叉树每一层结点
- 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-面试算法经典-Java实现】【116-Populating Next Right Pointers in Each Node(二叉树链接右指针)】
- Leetcode 树 Populating Next Right Pointers in Each Node II
- 【LeetCode-面试算法经典-Java实现】【117-Populating Next Right Pointers in Each Node(二叉树链接右指针II)】
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- LeetCode-116. Populating Next Right Pointers in Each Node【二叉树同层节点构成链表】
- Populating Next Right Pointers in Each Node II 任意(非完美)二叉树添加next指针 @LeetCode
- [C++]LeetCode: 124 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
- 【数字题1】股票的最大收益
- 关于Application.DoEvents()
- 几种浏览器脚本
- const用法详解(转)
- Cloning a running Virtual Machine using the Service Console
- leetcode:Populating Next Right Pointers in Each Node II (顺序连接二叉树每一层节点)【面试算法题】
- android TextView中图片和文字的灰显
- 我的编程生涯正式开始
- 类string的构造函数、拷贝构造函数和析构函数
- JQuery 选择和过滤方法代码总结
- iwconfig iwlist 用法
- jsp-jstl-学习
- Java 生成二维码
- Spring定时器任务