描述
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced
BST.

#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);}
}
#ifndef LISTNODE_H_
#define LISTNODE_H_
#include<iostream>
#include<vector>;
using namespace std;
struct node
{int data;node *next;
};
node *CreateNode(int a[], int n);
void DisplayNode(node *head);
int ListLength(node*head);
void DeleteNode(node *head);
node *nth_node(node *head,int len);#endif#include"ListNode.h"
node *CreateNode(int a[], int n)
{if (n <= 0) return NULL;node *head = new node();head->data = a[0];node *p = head;for (int i = 1; i < n; i++){node *q = new node();q->data = a[i];p->next = q;p = q;}p->next = NULL;return  head;
}
void DisplayNode(node *head)
{node *p = head;while (p){cout << p->data << " ";p = p->next;}cout << endl;
}
int ListLength(node *head)
{node *p = head;int length = 0;while (p){length++;p = p->next;}return  length;}
void DeleteNode(node *head)
{node *p = head;while (head){head = p->next;delete p;p = head;}
}
node *nth_node(node *head, int len)
{if (len <= 0 || len > ListLength(head))return NULL;node *p = head;for (int i = 1; i < len; i++){p = p->next;}return p;
}
#include "BinaryTree.h"
#include"ListNode.h"
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;//===由有序链表构建BST
BinaryTreeNode *ConSortListToBST(node *head, int len)
{if (len == 0)return NULL;if (len==1)return CreateBinaryTreeNode(head->data);node *temp = nth_node(head,len / 2 + 1);BinaryTreeNode *proot = CreateBinaryTreeNode(temp->data);proot->m_pLeft = ConSortListToBST(head, len / 2);proot->m_pRight = ConSortListToBST(nth_node(head, len / 2 + 2), (len-1)/2);return proot;
}
BinaryTreeNode *ConSortListToBST(node *head)
{int length = ListLength(head);return ConSortListToBST(head, length);
}int main()
{const int n = 6;int a[n] = { 1, 3, 5, 7, 9, 11};node *head = CreateNode(a, n);BinaryTreeNode *proot = ConSortListToBST(head);//===PrintTree(proot);//===DestroyTree(proot);DeleteNode(head);
}

5.3.5—二叉查找树—Convert Sorted List to Binary Sear Tree相关推荐

  1. 5.3.4—二叉查找树—Convert Sorted Array to Binary Sear Tree

    描述 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...

  2. LeetCode: 109. Convert Sorted List to Binary Search Tree

    题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height b ...

  3. Convert Sorted Array to Binary Search Tree - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Convert Sorted Array to Binary Search Tree - LeetCode 注意点 不要访问空结点 题目要求的是平衡二叉搜 ...

  4. leetcode -day19 Convert Sorted List to Binary Search Tree

    1.  Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted ...

  5. LeetCode: Convert Sorted Array to Binary Search Tree 解题报告

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  6. 【leetcode】109. Convert Sorted List to Binary Search Tree

    题目如下: Given a singly linked list where elements are sorted in ascending order, convert it to a heigh ...

  7. 【链表递归构造二叉树】LeetCode 109. Convert Sorted List to Binary Search Tree

    LeetCode 109. Convert Sorted List to Binary Search Tree Solution1:我的答案 偷鸡摸狗的做法 /*** Definition for s ...

  8. 【数组递归构造二叉树】LeetCode 108. Convert Sorted Array to Binary Search Tree

    LeetCode 108. Convert Sorted Array to Binary Search Tree Solution1:我的答案 构造二叉树利用递归 /*** Definition fo ...

  9. Convert Sorted List to Binary Search Tree ------C++ 递归创建平衡二叉查找树

    有序链表 0->1->2->3->4->5 转换为一个二叉排序树.我们在此创建一个平衡二叉排序树 1.先找链表到中间的节点 2.中间节点的val创建一个新的树节点Tree ...

最新文章

  1. java 获取sqlsession_获取Java的MyBatis框架项目中的SqlSession的方法
  2. mybatis 动态字段与表中不一样_mybatis创建一个或多个新用户 insert 字段和表名不确定时动态添加问题...
  3. Android中访问通讯录,数据的增删改查
  4. PHP学习笔记--面向对象
  5. Ubuntu 开发环境搭建
  6. Java并发编程—什么是线程?
  7. Andorid之教你全手工去除定制软件
  8. 在线进位制计算机,计算机基础知识进位计数制.pdf
  9. POE交换机技术及优势介绍
  10. 6、Flutter Error waiting for a debug connection: ProcessException: adb did not report f(转)
  11. Spring Boot 热启动
  12. 和硬件交换方面的知识
  13. 嵩天python测验_北理 嵩天老师 Python程序设计 测验易错题总结
  14. MySQL区分大小写
  15. Activiti7工作流引擎介绍
  16. Python环境下数据处理常用命令
  17. b560主板怎么样 b560主板支持的cpu
  18. 乐固加固后windows下实现给apk签名
  19. 单链表的算法之尾部插入节点
  20. 福建师范大学网络教育学院 计算机应用基础 第三次作业,福建师范大学网络教育学院_《计算机应用基础》第三次作业...

热门文章

  1. JAVA工作日历天数(工作日/非工作日)- 基于法定节假日
  2. 失恋CPR 自救手册
  3. 解决iPhone连接电脑,照片每次弹出
  4. SpaceShooter打飞机教程笔记(三)
  5. Java Web HTML基础 静态网页制作
  6. 【邻接表】77 邻接表:顶点u的下一个邻接点
  7. [爱情智慧]爱作的女人,最后都不怎么好!学会述情才能婚姻幸福!
  8. 四旋翼无人机避障飞行
  9. 用Python 80行代码实现一个微信消息撤回捕捉功能
  10. 利用人脸微笑数据集训练识别模型,完成对人脸图片微笑识别