5.1.12—二叉树的遍历—Populating Next Right Pointers in Ea Node II
来源:互联网 发布:开淘宝网店详细步骤 编辑:程序博客网 时间:2024/06/06 18:30
描述
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
Aer calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
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
Aer calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL
#pragma once#include<iostream>struct BinaryTreeNode{int m_nValue;BinaryTreeNode* m_pLeft;BinaryTreeNode* m_pRight;};BinaryTreeNode* CreateBinaryTreeNode(int value);void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight);void PrintTreeNode(BinaryTreeNode* pNode);void PrintTree(BinaryTreeNode* pRoot);void DestroyTree(BinaryTreeNode* pRoot);
#include "BinaryTree.h"BinaryTreeNode* CreateBinaryTreeNode(int value){BinaryTreeNode* pNode = new BinaryTreeNode();pNode->m_nValue = value;pNode->m_pLeft = NULL;pNode->m_pRight = NULL;return pNode;}void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight){if (pParent != NULL){pParent->m_pLeft = pLeft;pParent->m_pRight = pRight;}}void PrintTreeNode(BinaryTreeNode* pNode){if (pNode != NULL){printf("value of this node is: %d\n", pNode->m_nValue);if (pNode->m_pLeft != NULL)printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);elseprintf("left child is null.\n");if (pNode->m_pRight != NULL)printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);elseprintf("right child is null.\n");}else{printf("this node is null.\n");}printf("\n");}void PrintTree(BinaryTreeNode* pRoot){PrintTreeNode(pRoot); if (pRoot != NULL){if (pRoot->m_pLeft != NULL)PrintTree(pRoot->m_pLeft);if (pRoot->m_pRight != NULL)PrintTree(pRoot->m_pRight);}}void DestroyTree(BinaryTreeNode* pRoot){if (pRoot != NULL){BinaryTreeNode* pLeft = pRoot->m_pLeft;BinaryTreeNode* pRight = pRoot->m_pRight;delete pRoot;pRoot = NULL;DestroyTree(pLeft);DestroyTree(pRight);}}
#include "BinaryTree.h"#include <stack>#include<queue>#include<vector>using namespace std;//===二叉树的层次遍历vector<vector<int>> LevelTraversal(BinaryTreeNode *proot){queue<BinaryTreeNode*>temp;vector<int> tempres;//记录所有数字vector<int> num;//记录每一层的个数temp.push(proot);int Initial = 1;num.push_back(Initial);while (!temp.empty()){int cnt = 0;for (int i = 0; i < Initial; i++){BinaryTreeNode *p = temp.front();tempres.push_back(p->m_nValue);temp.pop();if (p->m_pLeft){temp.push(p->m_pLeft);cnt++;}if (p->m_pRight){temp.push(p->m_pRight);cnt++;}}Initial = cnt;if (Initial!=0)num.push_back(Initial);}//====================vector<vector<int>> res;int cnt = 0;for (int i = 0; i < num.size();i++){vector<int> cahe;for (int j =cnt; j < cnt+num[i]; j++)cahe.push_back(tempres[j]);cnt += num[i];res.push_back(cahe);}return res;}// ====================测试代码====================// 8// 6 10// 5 7 9 11int main(){BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);ConnectTreeNodes(pNode8, pNode6, pNode10);ConnectTreeNodes(pNode6, pNode5, pNode7);ConnectTreeNodes(pNode10, pNode9, pNode11);//===//PrintTree(pNode8);//===vector<vector<int>> res = LevelTraversal(pNode8);for (int i = 0; i < res.size(); i++){for (int j = 0; j < res[i].size(); j++)cout << res[i][j] << " ";cout << endl;}DestroyTree(pNode8);}
阅读全文
0 0
- 5.1.12—二叉树的遍历—Populating Next Right Pointers in Ea Node II
- 5.4.7—二叉树的递归—Populating Next Right Pointers in Ea Node
- 【二叉树&层次遍历】Populating Next Right Pointers in Each Node II
- 5.1.12 Populating Next Right Pointers in Each Node II
- LeetCode117—Populating Next Right Pointers in Each Node II
- 遍历 Populating Next Right Pointers in Each Node II
- 5.1.10 Populating Next Right Pointers in Each Node II
- Leetcode 树 Populating Next Right Pointers in Each Node II
- 树——populating-next-right-pointers-in-each-node(层序遍历变型)
- Populating Next Right Pointers in Each Node II 顺序链接二叉树每一层结点
- [C++]LeetCode: 124 Populating Next Right Pointers in Each Node II(链接二叉树)
- populating-next-right-pointers-in-each-node-ii(二叉树每层用指针链接)
- 【二叉树&层次遍历】Populating Next Right Pointers in Each Node
- 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 II
- 116.Populating Next Right Pointers in Each Node(二叉完全树的next结点)
- Leetcode 117 Populating Next Right Pointers in Each Node II 二叉树填充next指针指向右侧结点 II
- Populating Next Right Pointers in Each Node II 任意(非完美)二叉树添加next指针 @LeetCode
- 5.1.11—二叉树的遍历—Flatten Binary Tree to Linked List
- ?
- ConcurrentHashMap
- springmvc笔记-7-文件上传下载,拦截器
- hdu 6070 Dirt Ratio(二分+线段树)(2017 Multi-University Training Contest
- 5.1.12—二叉树的遍历—Populating Next Right Pointers in Ea Node II
- 开发环境配置(hibernate4)和简单增删改查
- php使用phpexcel类操作excel文件数据
- docker~从Dockerfile到Container的过程(终于算是OK了)
- 5.2.1—二叉树的构建—Construct Binary Tree from Preorder and Inorder Traversal
- 数据类型,引用数据类型
- Codeforces837C two seals
- 快速查找素数 nyoj 187
- 在我眼中vue/vux的钩子