Convert Sorted List to Binary Search TreeOct 3 '125768 / 16298
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode *sortedListToBST(ListNode *head) { if (head == NULL) return NULL; ListNode* t = head; int len = 0; while (t != NULL) len++, t = t->next; return gen(head, 0, len - 1); } TreeNode *gen(ListNode* &cur, int start, int end) { if (start > end) return NULL; int mid = start + (end -start) / 2; TreeNode *left = gen(cur, start, mid - 1); TreeNode *node = new TreeNode(cur->val); cur = cur->next; node->left = left; node->right = gen(cur, mid+1, end); } };
相关推荐
leetcode的题目:Balanced Binary Tree
lru缓存leetcode leetcode 大批 41. First Missing Positive 广度优先搜索 773. Sliding Puzzle 864. Shortest Path to Get All Keys 深度优先搜索 996. Number of Squareful Arrays 拓扑排序 269. Alien Dictionary...
leetcode卡leetcode 二叉树卡片 LeetCode 二叉树卡片问题的章节智解
Convert Sorted List to Binary Search Tree LCA of BST Kth Smallest Element in a BST 二叉树的递归 Minimum Depth of Binary Tree Maximum Depth of Binary Tree Path Sum Path Sum II Binary Tree Maximum Path ...
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
leetcode 不会二叉树倾斜 给定一棵二叉树,返回整棵树的倾斜度。 树节点的倾斜度定义为所有左子树节点值的总和与所有右子树节点值的总和之间的绝对差。 空节点的倾斜度为 0。 整棵树的倾斜度定义为所有节点倾斜度的...
leetcode lintcode差异 Lintcode 解题思路记录 Table of Contents Linked List Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ascending order, convert it ...
leetcode卡leetcode_practices_learncard_binarytree 我的 leetcode 练习二叉树学习卡在 100% 完成 :) 我的 Java8 备忘单:
leetcode卡除非您已经使用过卡片,否则不要看这里。 做真实的自己!
leetcode 不会LeetCode_563--二叉树倾斜 给定一棵二叉树,返回整棵树的倾斜度。 树节点的倾斜度定义为所有左子树节点值的总和与所有右子树节点值的总和之间的绝对差。 空节点的倾斜度为 0。 整棵树的倾斜度定义为...
leetcode卡 leetcode exercises 3-5 solutions everyday. fighting~ TODO array Best Time to Buy and Sell Stock II Valid Sudoku linked list Palindrome linked list Linked List Cycle trees Convert Sorted ...
LeetCode Merge 2 Sorted Lists解决方案
BinaryTree.py是一个方便的工具,它可以构建和显示编码时所需的二叉树 演示 您需要在使用之前导入该类 import BinaryTree as bt 从值/对象列表或二叉树构造二叉树 t1 = bt.BinaryTree([1,2,3,4,5,'#',6,7,'#','#','#...
编程问题 , , 等问题的Java解决方案。每个问题都附带说明和解决方案。法昂包含FAANG(Facebook,Amazon,Apple,Netflix和Google)公司用于采访的编程问题。 至少那是互联网所说的... 此软件包中不仅存在FAANG问题...
704.Binary_Search二分查找【LeetCode单题讲解系列】
Programming Questions on BinarySearch, LeetCode, CodeChef
二分查找Binary_Search套路和解题模板【LeetCode刷题套路教程3】
leetcode 数据结构题目中的答案,已经调试,直接运行,求二叉树的最小深度
leetcode伪代码convert-binary-number-in-a-linked-list-to-integer 题目解读: 题目来源: 原文: Given head which is a reference node to a singly-linked list. The value of each node in the linked list is ...
144.Binary_Tree_Preorder_Traversal二叉树的前序遍历【LeetCode单题讲解系列】