In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you how to verify the binary search tree in python, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Given a binary tree, determine whether it is a valid binary search tree.
Suppose a binary search tree has the following characteristics:
The left subtree of a node contains only a number less than the current node.
The right subtree of a node contains only a number greater than the current node.
All left and right subtrees must also be binary search trees themselves.
Example 1:
Enter:
two
/\
1 3
Output: true
Example 2:
Enter:
five
/\
1 4
/\
3 6
Output: false
Explanation: the input is: [5pr 1pr 4pr nullpr nullpr 3pr 6].
The root node has a value of 5, but its right child node has a value of 4.
Ideas for solving the problem:
1, traversal in the middle order
/ * Definition for a binary tree node. * type TreeNode struct {* Val int * Left * TreeNode * Right * TreeNode * * / var last= ^ (int (^ uint (0) > > 1) func isValidBST (root * TreeNode) bool {if rootworthy ValidBST (root.Left) {return false} if last > = root.Val {return false} last=root.Val ifValidBST ( Root.Right) {return false}} return true}
Method 2:
Recursion: the root node > is greater than the maximum value of the left node and less than the minimum value of the right node
/ * Definition for a binary tree node. * type TreeNode struct {* Val int * Left * TreeNode * Right * TreeNode * * / func isValidBST (root * TreeNode) bool {if root==nil {return true} if root.LeftExpenditure return isValidBST (root.Left) & & isValidBST (root.Right) & & l
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.