zoukankan      html  css  js  c++  java
  • 0701二叉搜索树的插入操作 Marathon

    给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。

    注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果 。

    示例 1:

    输入:root = [4,2,7,1,3], val = 5
    输出:[4,2,7,1,3,5]
    解释:另一个满足题目要求可以通过的树是:

    示例 2:

    输入:root = [40,20,60,10,30,50,70], val = 25
    输出:[40,20,60,10,30,50,70,null,null,25]
    示例 3:

    输入:root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
    输出:[4,2,7,1,3,5]

    提示:

    给定的树上的节点数介于 0 和 10^4 之间
    每个节点都有一个唯一整数值,取值范围从 0 到 10^8
    -10^8 <= val <= 10^8
    新值和原始二叉搜索树中的任意节点值都不同

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/insert-into-a-binary-search-tree

    参考:

    python

    # 0701.二叉搜索树的插入操作
    
    class Solution1:
        def insertIntooBST(self, root: TreeNode, val: int) -> TreeNode:
            """
            递归法-有返回值
            :param root:
            :param val:
            :return:
            """
            # 节点空,即找到合适位置插入
            if not root:
                return TreeNode(val)
            # 递归构建左右子树
            if root.val < val:
                root.right = self.insertIntoBST(root.right, val)
            if root.val > val:
                root.left = self.insertIntoBST(root.left, val)
    
            return root
    
    class Solution2:
        def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
            """
            递归法-无返回值
            :param root:
            :param val:
            :return:
            """
            if not root:
                return TreeNode(val)
    
            parent = None
    
            def travel(cur: TreeNode, val: int) -> None:
                # 函数运行的同时将新节点插入到合适位置-填充到空节点
                nonlocal parent
                if not cur:
                    newNode = TreeNode(val)
                    if parent.val < val:
                        parent.right = newNode
                    else:
                        parent.left = newNode
                    return
                parent = cur # parent作用只有运行到上面的if not cur才能体现
                if cur.val < val:
                    travel(cur.right, val)
                else:
                    travel(cur.left, val)
                return
    
            travel(root, val)
            return root
    
    class Solution3:
        def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
            if not root:
                return TreeNode(val)
    
            parent = None
            cur = root
    
            # while循环不断找新节点的parent
            while cur:
                if cur.val < val:
                    parent = cur
                    cur = cur.right
                elif cur.val > val:
                    parent = cur
                    cur = cur.left
    
            # 新节点的parent已经找到,新节点ready,插入节点
            if parent.val > val:
                parent.left = TreeNode(val)
            else:
                parent.right = TreeNode(val)
    
            return root
    
    
    

    golang

    package binaryTree
    
    // 递归法
    func insertIntoBST(root *TreeNode, val int) *TreeNode {
    	if root == nil {
    		root = &TreeNode{Val:val}
    		return root
    	}
    	if root.Val > val {
    		root.Left = insertIntoBST(root.Left, val)
    	} else {
    		root.Right = insertIntoBST(root.Right, val)
    	}
    	return root
    }
    
    // 迭代法
    func inserIntoBST1(root *TreeNode, val int) *TreeNode {
    	if root == nil {
    		return &TreeNode{Val: val}
    	}
    	node := root
    	var pNode *TreeNode
    	for node != nil {
    		if node.Val < val {
    			pNode = node
    			node = node.Right
    		} else {
    			pNode = node
    			node = node.Left
    		}
    	}
    	if pNode.Val < val {
    		pNode.Right = &TreeNode{Val: val}
    	} else {
    		pNode.Left = &TreeNode{Val: val}
    	}
    	return root
    }
    
    
    
  • 相关阅读:
    Java中Runnable和Thread的区别
    git 代理设置
    Android的bitmap和优化
    String、StringBuffer与StringBuilder之间区别
    工作流的一些记录
    UIAutomation调用计算器模拟自动执行
    从客户端(Content="<EM ><STRONG ><U >这是测试这...")中检测到有潜在危险的Request.Form 值。
    泛型
    基础加强
    数据库和ado
  • 原文地址:https://www.cnblogs.com/davis12/p/15579556.html
Copyright © 2011-2022 走看看