LeetCode_CC150
  • Introduction
  • LeetCode
    • Single Number
    • Contains Duplicate
    • Happy Number
    • Valid Anagram
    • Contains Duplicate II
    • Count Primes
    • Isomorphic Strings
    • Word Pattern
    • Island Perimeter
    • Find the Difference
    • Palindrome Permutation
    • Two Sum III - Data structure design
    • Number of Boomerangs
    • Longest Palindrome
    • Logger Rate Limiter
    • Find All Anagrams in a String
    • Keyboard Row
    • Distribute Candies
    • Shortest Word Distance
    • Majority Element
    • Plus One
    • Best Time to Buy and Sell Stock
    • Best Time to Buy and Sell Stock II
    • Pascal's Triangle
    • Remove Element
    • Rotate Array
    • Pascal's Triangle II
    • Two Sum II - Input array is sorted
    • Third Maximum Number
    • Max Consecutive Ones
    • K-diff Pairs in an Array
    • Maximum Product of Three Numbers
    • Maximum Distance in Arrays
    • Shortest Unsorted Continuous Subarray
    • Roman to Integer
    • Count and Say
    • Valid Parentheses
    • Longest Common Prefix
    • Valid Palindrome
    • Length of Last Word
    • Repeated Substring Pattern
    • Number of Segments in a String
    • Valid Word Abbreviation
    • Longest Uncommon Subsequence I
    • Student Attendance Record I
    • Reverse Words in a String III
    • Arranging Coins
    • Guess Number Higher or Lower
    • Search Insert Position
    • Min Stack
    • Diameter of Binary Tree
    • Unique Binary Search Trees
    • Unique Binary Search Trees II
    • Binary Tree Zigzag Level Order Traversal
    • Nim Game
    • Add Digits
    • Fizz Buzz
    • Climbing Stairs
    • Array Partition I
    • Power of Three
    • Power of Four
    • Power of Two
    • Ugly Number
    • Find All Numbers Disappeared in an Array
    • Find All Duplicates in an Array
    • Minimum Moves to Equal Array Elements
    • Meeting Rooms
    • Subsets
    • Subsets II
    • Count Complete Tree Nodes
    • Minimum Size Subarray Sum
    • Maximum Size Subarray Sum Equals k
    • Sparse Matrix Multiplication
    • Meeting Rooms II
    • Letter Combinations of a Phone Number
    • Binary Tree Vertical Order Traversal
    • Find the Celebrity
    • Merge Intervals
    • One Edit Distance
    • Multiply Strings
  • Array&String
    • Subarray Sum
    • Maximum Subarray
    • Intersection of Two Arrays
    • Intersection of Two Arrays II
    • Partition List
    • Merge Sorted Array
    • Two Sum
    • 3Sum
    • Product of Array Except Self
    • Rotate Image
    • Spiral Matrix
  • Linked List
    • Merge Two Sorted Lists
    • Insert into a Cyclic Sorted List
    • Sort List
    • Linked List Cycle
    • Copy List with Random Pointer
    • Add Two Numbers
    • Delete Node in a Linked List
    • Reverse Linked List
    • Odd Even Linked List
    • Intersection of Two Linked Lists
    • Palindrome Linked List
    • Insertion Sort List
    • Remove Linked List Elements
    • Remove Duplicates from Sorted List
    • Swap Nodes in Pairs
    • Remove Nth Node From End of List
  • Binary Search
    • Missing Number
    • Valid Perfect Square
    • 744. Find Smallest Letter Greater Than Target
    • Sqrt(x)
    • First Bad Version
    • Pow(x, n)
    • Find the Duplicate Number
    • Find Minimum in Rotated Sorted Array
    • Find Minimum in Rotated Sorted Array II
    • Total Occurrence of Target
    • Search in a Big Sorted Array
    • Longest Increasing Subsequence
    • Find Peak Element
    • Search in Rotated Sorted Array
    • Search a 2D Matrix
    • Search a 2D Matrix II
    • Closest Number in Sorted Array
    • Search in Rotated Sorted Array II
    • Search for a Range
    • Maximum Number in Mountain Sequence
    • Last Position of Target
    • K Closest Numbers In Sorted Array
    • Sqrt(x) II
  • Binary Tree
    • Maximum Depth of Binary Tree
    • Invert Binary Tree
    • Same Tree
    • Binary Tree Paths
    • Lowest Common Ancestor of a Binary Search Tree
    • Balanced Binary Tree
    • Convert Sorted Array to Binary Search Tree
    • Symmetric Tree
    • Path Sum
    • Minimum Depth of Binary Tree
    • Binary Tree Preorder Traversal
    • Binary Tree Inorder Traversal
    • Binary Tree Level Order Traversal
    • Binary Tree Level Order Traversal II
    • Minimum Subtree
    • Flatten Binary Tree to Linked List
    • Binary Tree Longest Consecutive Sequence
    • Subtree with Maximum Average
    • Number of Islands
    • Serialize and Deserialize Binary Tree
    • Clone Graph
  • Data Structure
    • Hash Table
    • Bubble Sort
    • Selection Sort
    • Binary Search
    • Merge Sort
    • Binary Tree
    • 递归
    • DFS BFS
    • python技巧
  • two pointers
    • Reverse Vowels of a String
    • Reverse String
    • Remove Duplicates from Sorted Array
    • LeetCode 11. Container With Most Water
    • Strobogrammatic Number
    • Move Zeroes
    • Implement strStr()
  • 哈希表
    • Ransom Note
    • Minimum Index Sum of Two Lists
    • Longest Harmonious Subsequence
    • Untitled
Powered by GitBook
On this page
  • 树的遍历
  • 树类题的复杂度分析
  • Binary Search Tree - 二叉查找树

Was this helpful?

  1. Data Structure

Binary Tree

PreviousMerge SortNext递归

Last updated 5 years ago

Was this helpful?

二叉树的第i层至多有 2^(i-1) 个结点;深度为k的二叉树至多有 2^k-1 个结点

树的遍历

从二叉树的根节点出发,节点的遍历分为三个主要步骤:对当前节点进行操作(称为“访问”节点,或者根节点)、遍历左边子节点、遍历右边子节点。访问节点顺序的不同也就形成了不同的遍历方式。需要注意的是树的遍历通常使用递归的方法进行理解和实现,在访问元素时也需要使用递归的思想去理解。实际实现中对于前序和中序遍历可尝试使用递归实现。

按照访问根元素(当前元素)的前后顺序,遍历方式可划分为如下几种:

  • 深度优先:先访问子节点,再访问父节点,最后访问第二个子节点。根据根节点相对于左右子节点的访问先后顺序又可细分为以下三种方式。

    1. 前序(pre-order):先根后左再右

    2. 中序(in-order):先左后根再右

    3. 后序(post-order):先左后右再根

  • 广度优先:先访问根节点,沿着树的宽度遍历子节点,直到所有节点均被访问为止。

如下图所示,遍历顺序在右侧框中,红色A为根节点。使用递归和整体的思想去分析遍历顺序较为清晰。

二叉树的广度优先遍历和树的前序/中序/后序遍历不太一样,前/中/后序遍历使用递归,也就是栈的思想对二叉树进行遍历,广度优先一般使用队列的思想对二叉树进行遍历。

如果已知中序遍历和前序遍历或者后序遍历,那么就可以完全恢复出原二叉树结构。其中最为关键的是前序遍历中第一个一定是根,而后序遍历最后一个一定是根,中序遍历在得知根节点后又可进一步递归得知左右子树的根节点。但是这种方法也是有适用范围的:元素不能重复!否则无法完成定位。

class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None

class Traversal(object):
    def __init__(self):
        self.traverse_path = list()

    def preorder(self, root):
        if root:
            self.traverse_path.append(root.val)
            self.preorder(root.left)
            self.preorder(root.right)

    def inorder(self,root):
        if root:
            self.inorder(root.left)
            self.traverse_path.append(root.val)
            self.inorder(root.right)

    def postorder(self,root):
        if root:
            self.postorder(root.left)
            self.postorder(root.right)
            self.traverse_path.append(root.val)

树类题的复杂度分析

对树相关的题进行复杂度分析时可统计对每个节点被访问的次数,进而求得总的时间复杂度。

Binary Search Tree - 二叉查找树

一颗二叉查找树(BST)是一颗二叉树,其中每个节点都含有一个可进行比较的键及相应的值,且每个节点的键都大于等于左子树中的任意节点的键,而小于右子树中的任意节点的键。

使用中序遍历可得到有序数组,这是二叉查找树的又一个重要特征。

二叉查找树使用的每个节点含有两个链接,它是将链表插入的灵活性和有序数组查找的高效性结合起来的高效符号表实现。