【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 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: 5You can return this binary search tree:
4
/ \
2 7
/ \ /
1 3 5This 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的更多相关文章
- 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- 【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 ...
- 【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. 有序 ...
- 【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 ...
- 【题解】【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.思路: ...
- 【leetcode】501. Find Mode in Binary Search Tree
class Solution { public: vector<int> modes; int maxCnt = 0; int curCnt = 0; int curNum = 0; ve ...
- [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 ...
随机推荐
- sql中使一个字段升序,一个字段降序
ORDER BY _column1, _column2; /* _column1升序,_column2升序 */ ORDER BY _column1, _column2 DESC; /* _colum ...
- jenkins 更改端口
方法一 在Jenkins目录下,运行一下命令: java -jar jenkins.war --ajp13Port=-1 --httpPort=8081 出现了错误: C:\Program Files ...
- struts2---访问WEB
一:在Action中,可以通过以下方式访问WEB的HttpSession,HttpServletRequest,HttpServletResponse等资源 与Servlet API解耦的访问方式 通 ...
- Windows系统启动iis方法详解
很多网友一般都用Windows 系统自带的iis服务器来配置web网站,在本地进行调试和修改后才正式上线.虽说操作不难,但是小白来说却无从下手,很多人根本不知道iss在哪,怎么启动,更谈不上配置或者其 ...
- [CSP-S模拟测试86]题解
好久没有写整套题的题解了呢……主要是这两天考试题愈发神仙 实在是超出了垃圾博主的能力范围啊QAQ A.异或 不难想到,如果我们得到了$[L,R]$中每一位上0和1的个数,那么答案即为$2 \times ...
- Understanding identities in IIS
Understanding identities in IIS https://support.microsoft.com/en-my/help/4466942/understanding-ident ...
- JAVA 大数开方模板
JAVA 大数开方模板 import java.math.BigInteger; import java.math.*; import java.math.BigInteger; import jav ...
- Nginx (限速)限制并发、限制访问速率、限制流量
Nginx 限制并发访问速率流量,配置还是简单的,看下Nginx文档根据文中这三个模块对照看一下就可以,Nginx限速使用的是漏桶算法(感兴趣可以看下文末的参考资料),需要注意的是:当需要进行限速操作 ...
- Shiro那些事儿(一): Shiro初探
引言 权限,可以简单的理解成你能干什么,不能干什么.在管理系统中,对权限的设计可以很简单,也可以很复杂.简单点的,基本都是基于角色扮演的方式,比如系统管理员角色可以操作哪些菜单,普通用户角色可以操作哪 ...
- Python字节码与解释器学习
参考:http://blog.jobbole.com/55327/ http://blog.jobbole.com/56300/ http://blog.jobbole.com/56761/ 1. 在 ...