题目如下:

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. 【FTP】FTP(文件传输协议)工作原理(SFTP)

    目前在网络上,如果你想把文件和其他人共享.最方便的办法莫过于将文件放FTP服务器上,然后其他人通过FTP客户端程序来下载所需要的文件. 1.FTP架构 如同其他的很多通讯协议,FTP通讯协议也采用客户 ...

  2. Nmon监控服务端性能

    一.安装1.查看服务器操作系统的版本信息 lsb_release -a cat /etc/*release2.下载 a.nmon下载地址:http://nmon.sourceforge.net/pmw ...

  3. Git使用包括切换分支

  4. 【HDOJ6611】K Subsequence(费用流)

    题意:给定一个长为n的正整数序列,要求从中取出至多k个不下降序列,使得它们的和最大,求这个和 n<=2e3,k<=10,a[i]<=1e5 思路:极其考验模板,反正我的spfa和zk ...

  5. [CSP-S模拟测试]:公园(BFS+剪枝)

    题目传送门(内部题31) 输入格式 第一行,五个整数$V,M,N,E,L$.接下来$M$行,每行两个正整数$s_i,a_i$.保证$s_i$互不相等.接下来$N$行,每行两个正整数$t_j,b_j$. ...

  6. python中遍历列表字典元组

    遍历列表,打印:我叫name,今年age岁,家住dizhi,电话phone lt = [ {'name':'小王', 'age':18, 'info':[('phone', '123'), ('diz ...

  7. SelfCert wcf中 生成x5.09证书的工具

    http://blog.pluralsight.com/selfcert-create-a-self-signed-certificate-interactively-gui-or-programma ...

  8. Git 提交的正确姿势

    Git 提交的正确姿势:Commit message 编写指南 SCOP范围 middleware core config plugin test type范围 Git 每次提交代码,都要写 Comm ...

  9. 使用iconv提示未知错误

    使用iconv 转化编码的时候提示错误:<b>Notice</b>: iconv() [<a href='http://www.jinyuanbao.cn'>fun ...

  10. [USACO 07NOV]电话线Telephone Wire

    题目描述 Farmer John's cows are getting restless about their poor telephone service; they want FJ to rep ...