码迷,mamicode.com
首页 > 其他好文 > 详细

lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点

时间:2015-10-12 20:57:26      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

题目:

 在二叉查找树中插入节点

给定一棵二叉查找树和一个新的树节点,将节点插入到树中。

你需要保证该树仍然是一棵二叉查找树。

 样例

给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的:

 技术分享技术分享技术分享

挑战

能否不使用递归?

解题:

递归的方法比较简单

Java程序:

技术分享
/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @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.
     */
    public TreeNode insertNode(TreeNode root, TreeNode node) {
        // write your code here
        if(root==null)
            return node;
        if(root.val>=node.val)
            root.left = insertNode(root.left,node);
        if(root.val<node.val)
            root.right = insertNode(root.right,node);
        return root;
    }
}
View Code

总耗时: 1636 ms

非递归的感觉比较复杂。。。。

一直非递归的方法,就是一直走,一直走,走到没有路的时候就是插入的节点,这里是因为插入的节点一定是在新建的叶子节点,原理是二叉查找树是;1,根节点左子树的值比根节点小,右子树的值都比根节点大,2.左右子树也满足1的条件

下面程序定义的last指针是用来做最后插入节点的父节点的

 

技术分享
/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @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.
     */
    public TreeNode insertNode(TreeNode root, TreeNode node) {
        // write your code here
        if(root==null){
             root=node;
             return root;
        }
        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;
            }
        }
        return root;
    }
}
View Code

 

总耗时: 1753 ms

Python程序:

技术分享
"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""
class Solution:
    """
    @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.
    """
    def insertNode(self, root, node):
        # write your code here
        if root==None:
            return node
        if root.val>=node.val:
            root.left = self.insertNode(root.left,node)
        if root.val<node.val:
            root.right = self.insertNode(root.right,node)
        return root
        
View Code

总耗时: 272 ms

非递归程序:

 

技术分享
"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""
class Solution:
    """
    @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.
    """
    def insertNode(self, root, node):
        # write your code here
        cur = root
        last = None
        if root==None:
            return node
        while cur!=None:
            last = cur
            if cur.val>node.val:
                cur = cur.left
            elif cur.val<=node.val:
                cur = cur.right
        if last!=None:
            if last.val>node.val:
                last.left = node
            else:
                last.right = node
        return root
View Code

 

lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点

标签:

原文地址:http://www.cnblogs.com/theskulls/p/4872243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!