二元樹

二元樹題型的 LeetCode 解題教學,11 題。遞迴寫法先想清楚「回傳什麼」,迭代寫法就是手動維護堆疊——兩種都會寫。

Lowest Common Ancestor of a Binary Search Tree 解題教學示意圖|LeetCode 235

Lowest Common Ancestor of a Binary Search Tree 解題教學:3 種解法完整比較|LeetCode 235 Best Practice

Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.

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).”

Lowest Common Ancestor of a Binary Search Tree 解題教學:3 種解法完整比較|LeetCode 235 Best Practice Read More »

Leetcode, 技術文章, , , ,
Kth Smallest Element in a BST 解題教學示意圖|LeetCode 230

Kth Smallest Element in a BST 解題教學:3 種解法完整比較|LeetCode 230 Best Practice

Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.

Kth Smallest Element in a BST 解題教學:3 種解法完整比較|LeetCode 230 Best Practice Read More »

Leetcode, 技術文章, , , , ,
Validate Binary Search Tree 解題教學示意圖|LeetCode 98

Validate Binary Search Tree 解題教學:3 種解法完整比較|LeetCode 98 Best Practice

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.

Validate Binary Search Tree 解題教學:3 種解法完整比較|LeetCode 98 Best Practice Read More »

Leetcode, 技術文章, , , ,
Construct Binary Tree from Preorder 解題教學示意圖|LeetCode 105

Construct Binary Tree from Preorder 解題教學:3 種解法完整比較|LeetCode 105 Best Practice

Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.

Construct Binary Tree from Preorder 解題教學:3 種解法完整比較|LeetCode 105 Best Practice Read More »

Leetcode, 技術文章, , , ,
Subtree of Another Tree 解題教學示意圖|LeetCode 572

Subtree of Another Tree 解題教學:3 種解法完整比較|LeetCode 572 Best Practice

Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise.

A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node’s descendants. The tree tree could also be considered as a subtree of itself.

Subtree of Another Tree 解題教學:3 種解法完整比較|LeetCode 572 Best Practice Read More »

Leetcode, 技術文章, , , ,
Serialize and Deserialize Binary Tree 解題教學示意圖|LeetCode 297

Serialize and Deserialize Binary Tree 解題教學:3 種解法完整比較|LeetCode 297 Best Practice

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

Serialize and Deserialize Binary Tree 解題教學:3 種解法完整比較|LeetCode 297 Best Practice Read More »

Leetcode, 技術文章, , , ,
Binary Tree Level Order Traversal 解題教學示意圖|LeetCode 102

Binary Tree Level Order Traversal 解題教學:3 種解法完整比較|LeetCode 102 Best Practice

Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).

Binary Tree Level Order Traversal 解題教學:3 種解法完整比較|LeetCode 102 Best Practice Read More »

Leetcode, 技術文章, , , ,
Binary Tree Maximum Path Sum 解題教學示意圖|LeetCode 124

Binary Tree Maximum Path Sum 解題教學:3 種解法完整比較|LeetCode 124 Best Practice

A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.

The path sum of a path is the sum of the node’s values in the path.

Given the root of a binary tree, return the maximum path sum of any non-empty path.

Binary Tree Maximum Path Sum 解題教學:3 種解法完整比較|LeetCode 124 Best Practice Read More »

Leetcode, 技術文章, , , ,
Flip Equivalent Binary Trees 解題教學示意圖|LeetCode 951

Flip Equivalent Binary Trees 解題教學:3 種解法完整比較|LeetCode 951 Best Practice

For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.

A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations.

Given the roots of two binary trees root1 and root2, return true if the two trees are flip equivalent or false otherwise.

Flip Equivalent Binary Trees 解題教學:3 種解法完整比較|LeetCode 951 Best Practice Read More »

Leetcode, 技術文章, , , ,
Same Tree 解題教學示意圖|LeetCode 100

Same Tree 解題教學:3 種解法完整比較|LeetCode 100 Best Practice

Given the roots of two binary trees p and q, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

Same Tree 解題教學:3 種解法完整比較|LeetCode 100 Best Practice Read More »

Leetcode, 技術文章, , , ,