Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; next = null; } 7 * } 8 */ 9 /**10 * Definition for binary tree11 * public class TreeNode {12 * int val;13 * TreeNode left;14 *
Question:Enter a binary search tree and convert it into a sorted two-way linked list.You must not create any new node. You only need to adjust the pointer point.
Based on the provided ideas, I wrote a C ++ code that reads an integer from any sequence, creates a binary search tree, and then changes it to a linked list:
#include
Sample input:
10 5 14 45 64 3 7 12
This article sorted out the most commonly used Linux commands to facilitate the quick search of common commands
Command
Description
Ls
Listing directories
Ls-al
Use formatting to list hidden files
CD dir
Enter Directory dir
Cd
Enter Home directory
Pwd
Show current directory
mkdir dir
Create D
Similar to the sorted array. But we had to remember, the position of node not depends on the index. So we need does the more things:1. Ensure the pointer is moving (change value) in function call. So we need pass the pointer's pointer to the function.2. Get left branch First, then the pointer can move to the root node now.1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next
Given a singly linked list where elements is sorted in ascending order, convert it to a height balanced BST.Bst PublicTreeNode Sortedlisttobst (ListNode head) {TreeNode tree=NULL; if(Head = =NULL)returnTree; if(Head.next = =NULL)return NewTreeNode (Head.val); //find the middle Pionter; varWalker =Head; varRunner =Head; varPrev =NewListNode (-1); while(Runner! =NULL Runner.next! =NULL) {prev=Walker; Walker=Walker.next; Runner=Runner.next.next;
A pen exam on the Internet:
Enter a binary search tree and convert it into a sorted two-way linked list. You cannot create any new node, but you can only adjust the pointer direction, for example:10/\6 14/\/\4 8 12 16Convert to a two-way linked list 4
Write a recursionAlgorithmAs follows:
# Include
LeetCode Convert Sorted List to Binary Search Tree solution report, leetcodesortedGiven a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Generates a balanced binary tree from a given ordered linked list.Solution: the easiest way to solve the problem is to use an array to generate a binary tree. Find the middle
AjaxControlToolkit is a set of controls, you can automatically add text boxes, click the text box pop-up calendar, add watermark and other Ajax effects, including more than 40 controls, the implementation of the effect such as: http://www.asp.net/ajaxLibrary/ Ajaxcontroltoolkitsamplesite/default.aspx
Like Baidu search, according to user Input Automatic association vocabulary, with the help of AjaxControlToolkit Autocompleteextender control very simple
This article describes how to use js to simulate a fuzzy search drop-down list similar to Baidu or google. For more information, see
The Code is as follows:
// JavaScript DocumentFunction onChangehoverLi (thisLi ){$ ("# Searchtext" ).val(((thisli).html ());$ ("# Suggest_ul"). hide (0 );Validateform2 ();}$ (Function (){// Hide the drop-down li$ ("# Suggest_ul"). hide (0 );});// Ajax dynamic keyword acquis
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.