码农的自我修炼之路-----BST
今天終于申請(qǐng)了博客,在職業(yè)生涯即將開始的時(shí)侯,我要培養(yǎng)自己碼農(nóng)的基本素質(zhì)了,嘎嘎。養(yǎng)成寫技術(shù)博客的習(xí)慣,為自己,也為分享。新司機(jī)要開車了,請(qǐng)系好安全帶~吼吼吼吼吼!
今天刷了一條leetcode題,是關(guān)于BST(Binary Search Tree),BST具有的特性如下:
1. 左子樹的value都 小于 根節(jié)點(diǎn)的value。
2. 右子樹的value都 大于 根節(jié)點(diǎn)的value。
3. 左右子樹都是BST。
原題如下:
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys?less than?the node's key.
- The right subtree of a node contains only nodes with keys?greater than?the node's key.
- Both the left and right subtrees must also be binary search trees.
?
Example 1:
2/ \1 3Binary tree?[2,1,3], return true.
?
Example 2:
1/ \2 3Binary tree?[1,2,3], return false.
一般注意陷阱: 1. 在判斷時(shí),不僅要判斷左孩子小于父節(jié)點(diǎn),而是整個(gè)左子樹都小于父節(jié)點(diǎn)的value。
? ? ? ? ? ? ? ? ? ? ?2. 是嚴(yán)格的小于和大于,等于的時(shí)候是不滿足BST的。
轉(zhuǎn)載于:https://www.cnblogs.com/mokayy/p/5576079.html
總結(jié)
以上是生活随笔為你收集整理的码农的自我修炼之路-----BST的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML学习笔记--HTML的语法【1】
- 下一篇: typeof 和instanceof