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