Lintcode Easy title: Insert node in a binary search tree inserts nodes in the binary lookup trees

Source: Internet
Author: User
Tags lintcode

Topic:

Inserting nodes in a binary lookup tree

Given a binary lookup tree and a new tree node, insert the node into the tree.

You need to make sure that the tree is still a binary search tree.

Sample Example

Give the following binary search tree, after inserting node 6, this binary search tree can be this:

 

Challenge

Can I not use recursion?

Solving:

Recursive method is relatively simple

Java Program:

/*** Definition of TreeNode: * public class TreeNode {* public int val; * Public TreeNode left, right; * PU Blic TreeNode (int val) {* This.val = val; * This.left = This.right = null; *} *}*/ Public classSolution {/**     * @paramroot:the root of the binary search tree. * @paramNode:insert This node into the binary search tree *@return: The root of the new binary search tree. */     PublicTreeNode Insertnode (TreeNode root, TreeNode node) {//Write your code here        if(root==NULL)            returnnode; if(root.val>=node.val) Root.left=Insertnode (Root.left,node); if(root.val<node.val) Root.right=Insertnode (Root.right,node); returnRoot; }}
View Code

Total time: 1636 Ms

The feeling of non-recursion is more complicated ....

has been a non-recursive method, is to go all the way, always walk, go to No road when is inserted node, here is because the inserted node must be in the new leaf node, the principle is two forks to find the tree is; 1, the root node left subtree value is smaller than the root node, the right subtree value is larger than the root node, 2. The subtree also satisfies the 1 condition

The last pointer defined by the following program is used to do the final insertion of the node's parent node.

/*** Definition of TreeNode: * public class TreeNode {* public int val; * Public TreeNode left, right; * PU Blic TreeNode (int val) {* This.val = val; * This.left = This.right = null; *} *}*/ Public classSolution {/**     * @paramroot:the root of the binary search tree. * @paramNode:insert This node into the binary search tree *@return: The root of the new binary search tree. */     PublicTreeNode Insertnode (TreeNode root, TreeNode node) {//Write your code here        if(root==NULL) {root=node; returnRoot; } TreeNode cur=Root; TreeNode Last=NULL;  while(cur!=NULL) { last=cur; if(cur.val>node.val) {cur=Cur.left; }Else{cur=Cur.right; }        }        if(last!=NULL){            if(last.val>node.val) {Last.left=node; }Else{last.right=node; }        }        returnRoot; }}
View Code

Total time: 1753 Ms

Python program:

"""Definition of Treenode:class treenode:def __init__ (Self, val): Self.val = Val Self.left, self.right = None, none"""classSolution:"""@param root:the root of the binary search tree.    @param node:insert This node into the binary search tree.    @return: The root of the new binary search tree. """    defInsertnode (self, Root, node):#Write your code here        ifroot==None:returnnodeifroot.val>=Node.val:root.left=Self.insertnode (Root.left,node)ifroot.val<Node.val:root.right=Self.insertnode (Root.right,node)returnRoot
View Code

Total time: 272 ms

Non-recursive program:

"""Definition of Treenode:class treenode:def __init__ (Self, val): Self.val = Val Self.left, self.right = None, none"""classSolution:"""@param root:the root of the binary search tree.    @param node:insert This node into the binary search tree.    @return: The root of the new binary search tree. """    defInsertnode (self, Root, node):#Write your code hereCur =Root Last=Noneifroot==None:returnnode whilecur!=None:last=curifCur.val>Node.val:cur=Cur.leftelifcur.val<=Node.val:cur=Cur.rightiflast!=None:ifLast.val>Node.val:last.left=nodeElse: Last.right=nodereturnRoot
View Code

Lintcode Easy title: Insert node in a binary search tree inserts nodes in the binary lookup trees

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.