题目如下:

Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.

Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.

For example,

Given the tree:
4
/ \
2 7
/ \
1 3
And the value to insert: 5

You can return this binary search tree:

         4
/ \
2 7
/ \ /
1 3 5

This tree is also valid:

         5
/ \
2 7
/ \
1 3
\
4

解题思路:因为题目对插入后树的高度没有任何约束,所以最直接的方法就是对树进行遍历。如果遍历到的当前节点值大于给定值,判断其节点的左子节点是否存在:不存在则将给定值插入到其左子节点,存在则往左子节点继续遍历;如果小于,则同理,判断其右子节点。直到找到符合条件的节点为止。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
loop = True
def recursive(self,node,val):
if self.loop == False:
return
if node.val > val:
if node.left == None:
node.left = TreeNode(val)
self.loop = False
else:
self.recursive(node.left,val)
else:
if node.right == None:
node.right = TreeNode(val)
self.loop = False
else:
self.recursive(node.right,val) def insertIntoBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
if root == None:
return TreeNode(val)
self.loop = True
self.recursive(root,val)
return root

【leetcode】701. Insert into a Binary Search Tree的更多相关文章

  1. 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【leetcode】Convert Sorted List to Binary Search Tree

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  4. 【leetcode】Convert Sorted Array to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  5. 【leetcode】Convert Sorted Array to Binary Search Tree (easy)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序 ...

  6. 【leetcode】Convert Sorted List to Binary Search Tree (middle)

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  7. 【题解】【BST】【Leetcode】Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路: ...

  8. 【leetcode】501. Find Mode in Binary Search Tree

    class Solution { public: vector<int> modes; int maxCnt = 0; int curCnt = 0; int curNum = 0; ve ...

  9. [LeetCode] 701. Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

随机推荐

  1. 多线程模拟生产者消费者示例之BlockQueue

    public class Test { public static void main(String[] args){ //创建一个阻塞队列,边界为1 BlockingQueue<String& ...

  2. 阿里云数据库备份DBS商业化发布,数据库实时备份到OSS

    数据库备份DBS已于2018年5月17日正式商业化发布. 数据库备份(Database Backup,简称DBS)是为数据库提供连续数据保护.低成本的备份服务. 它可以为多种环境的数据提供强有力的保护 ...

  3. springmvc 的 @PathVariable

    @PathVariable映射 URL 绑定的占位符 通过 @PathVariable 可以将 URL 中占位符参数绑定到控 •制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@Path ...

  4. ES6中类Class的super关键字

    super 关键字,既可以当作函数使用,也可以当作对象使用.在这两种情况下,它的用法完全不同. 1.super当做函数使用 super 作为函数调用时,代表父类的构造函数.ES6 要求,子类的构造函数 ...

  5. 【Dart学习】--之Iterable相关方法总结

    一,概述 按顺序访问的值或元素的集合, List集合也是继承于Iterable List和Set也是Iterable,dart:collection库中同样有很多 部分Iterable集合可以被修改 ...

  6. Bugku | Easy_Re

    学到一个函数: _mm_storeu_si128((__m128i *)&v5, _mm_loadu_si128((const __m128i *)&xmmword_413E34)); ...

  7. YII 1.0 常用CURD写法

    <?php //yii1.0 curd简单写法 //查询 Yii::app()->db->createCommand($sql)->queryAll();//查询所有行数据 ​ ...

  8. python练习题返回列表中的第二小的元素的下标

    # 第三题:返回列表中的第二小的元素的下标 # 1.参数是一个列表,元素全部是整数 # 2.返回第二小的元素的下标 def seconde_min(lt): n = len(lt) if lt[0]& ...

  9. 3.tensorflow——NN

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data numClasses=10 inp ...

  10. “希希敬敬对”队软件工程第九次作业-beta冲刺第四次随笔

    “希希敬敬对”队软件工程第九次作业-beta冲刺第四次随笔 队名:  “希希敬敬对” 龙江腾(队长) 201810775001 杨希                   201810812008 何敬 ...