/images/gravatar2.jpg

Steven Lin

[Golang][Leetcode][BinaryTree]刷題系列-257-Binary Tree Paths

257. Binary Tree Paths Level : Easy 原題連結 :Click 題目 : Given the root of a binary tree, return all root-to-leaf paths in any order. A leaf is a node with no children. Example : Note Example 1: Input: root = [1,2,3,null,5] Output: [“1->2->5”,“1->3”] Example 2: Input: root = [1] Output: [“1”] 解題思路 : 這題可以使用DFS(Depth First Search)的概念,優先去走到最底,之後再回頭,前往另一個最底 這題用recursive的方式很好做 Recursive解法 time complexity: O(n) , space complexity: O(1) Runtime: 0 ms, faster than 100.

[Golang][Leetcode][BinaryTree]刷題系列-110-Balanced Binary Tree

110. Balanced Binary Tree Level : Easy 原題連結 :Click 題目 : Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree in which the left and right subtrees of every node differ in height by no more than 1. Example : Note Example 1: Input: root = [3,9,20,null,null,15,7] Output: true Example 2: Input: root = [1,2,2,3,3,null,null,4,4] Output: false

[Golang][Leetcode][BinaryTree]刷題系列-222-Count Complete Tree Nodes

222. Count Complete Tree Nodes Level : Medium 原題連結 :Click 題目 : Given the root of a complete binary tree, return the number of the nodes in the tree. According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

[Golang][Leetcode][BinaryTree]刷題系列-101-Symmetric Tree

101. Symmetric Tree Level : Easy 原題連結 :Click 題目 : Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). Example : Note Example 1: Input: root = [1,2,2,3,4,4,3] Output: true Example 2: Input: root = [1,2,2,null,3,null,3] Output: false 解題思路 : 這題需要我們辨認這個binary tree是否對稱 所以起始點並不在root,而是在 left = root.Left , right = root.Right作為起點 第一個level會比較 (left.Left , right.

[Golang][Leetcode][BinaryTree]刷題系列-226-Invert Binary Tree

226. Invert Binary Tree Level : Easy 原題連結 :Click 題目 : Given the root of a binary tree, invert the tree, and return its root. Example : Note Example 1: Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1] Example 2: Input: root = [2,1,3] Output: [2,3,1] Example 3: Input: root = [] Output: [] 解題思路 : 這題屬於binary tree的基本題,直接照著邏輯作即可! 我的解法 time complexity: O(n) , space complexity: O(1) Runtime: 0 ms, faster than 100.

[Golang][Leetcode][BinaryTree]刷題系列-117-Populating Next Right Pointers in Each Node II

117. Populating Next Right Pointers in Each Node II Level : Medium 原題連結 :Click 題目 : You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.