Data Structure: Tree
mind the data structure: here comes the tree
Recursively Traverse a Tree
- Top-down: node first, like a pre-order
- Bottom-up: like post-order
BST: Binary Search Tree
- Left sub-tree key <= parent node’s key
- Right sub-tree key >= parent node’s key
Or
left sub-tree key <= node key <= right sub-tree key
Validate
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
return self.BST(root, -2147483649, 2147483648)
def BST(self, root, min, max):
if root == None:
return True
if root.val <= min or root.val >= max:
return False
else
return self.BST(root.left, min, root.val ) and self.BST(root.right, root.val, max)
Planted:
by L Ma;
Dynamic Backlinks to
wiki/algorithms/data-structure-tree
:wiki/algorithms/data-structure-tree
Links to:L Ma (2018). 'Data Structure: Tree', Datumorphism, 03 April. Available at: https://datumorphism.leima.is/wiki/algorithms/data-structure-tree/.