/images/gravatar2.jpg

Steven Lin

[Golang][Leetcode][BinaryTree]刷題系列-236-Lowest Common Ancestor of a Binary Tree

236. Lowest Common Ancestor of a Binary Tree Level : Medium 原題連結 :Click 題目 : Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).

[Golang][Leetcode][BinaryTree]刷題系列-501-Find Mode in Binary Search Tree

501. Find Mode in Binary Search Tree Level : Easy 原題連結 :Click 題目 : Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it. If the tree has more than one mode, return them in any order. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the node’s key.

[Golang][Leetcode][BinaryTree]刷題系列-530-Minimum Absolute Difference in BST

530. Minimum Absolute Difference in BST Level : Easy 原題連結 :Click 題目 : Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree. Example : Note Example 1: Input: root = [4,2,6,1,3] Output: 1 Example 2: Input: root = [1,0,48,null,null,12,49] Output: 1 解題思路 : 這題的概念可以參考leetcode98題解利用inorder特性的解法 BST若用inorder遍歷就會形成一個由小到大的有序排列,利用這個特性這題會變得非常簡單

[Golang][Leetcode][BinaryTree]刷題系列-98-Validate Binary Search Tree

98. Validate Binary Search Tree Level : Medium 原題連結 :Click 題目 : Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid 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.

[Golang][Leetcode][BinaryTree]刷題系列-700-Search in a Binary Search Tree

700. Search in a Binary Search Tree Level : Easy 原題連結 :Click 題目 : You are given the root of a binary search tree (BST) and an integer val. Find the node in the BST that the node’s value equals val and return the subtree rooted with that node. If such a node does not exist, return null. Example : Note Example 1: Input: root = [4,2,7,1,3], val = 2 Output: [2,1,3]

[Golang][Leetcode][BinaryTree]刷題系列-617-Merge Two Binary Trees

617. Merge Two Binary Trees Level : Easy 原題連結 :Click 題目 : You are given two binary trees root1 and root2. Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node.