Leetcode#114. Flatten Binary Tree to Linked List
ProblemGiven the root of a binary tree, flatten the tree into a “linked list”:
The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
The “linked list” should be in the same order as a pre-order traversal of the binary tree.
Example 1:
!https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg
123Input: root = [1,2,5,3,4,null,6]Output: [1,null,2,null,3,null,4,null,5,null,6]
Example 2:
1 ...
Leetcode#450. Delete Node in a BST
ProblemGiven a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.
Basically, the deletion can be divided into two stages:
Search for a node to remove.
If the node is found, delete the node.
Example 1:
!https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg
123456Input: root = [5,3,6,2,4,null,7], key = 3Output: [5,4,6,2,null,null,7]Explanation: Given key to delete is 3. So we find the node ...
Leetcode#101. Symmetric Tree
ProblemGiven the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Example 1:
!https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg
123Input: root = [1,2,2,3,4,4,3]Output: true
Example 2:
!https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg
123Input: root = [1,2,2,null,3,null,3]Output: false
Constraints:
The number of nodes in the tree is in the range [1, 1000].
100 <= Node.val <= 100
Follow up:
Could you solve it both r ...
Leetcode#104. Maximum Depth of Binary Tree
ProblemGiven the root of a binary tree, return its maximum depth.
A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
!https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg
123Input: root = [3,9,20,null,null,15,7]Output: 3
Example 2:
123Input: root = [1,null,2]Output: 2
Constraints:
The number of nodes in the tree is in the range [0, 104].
100 <= Node.val <= 100
Solve解11234567891011121314151617 ...
Leetcode#124. Binary Tree Maximum Path Sum
ProblemA 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.
Example 1:
!https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg
1234Input: root = [1,2,3]Output: 6 ...
Leetcode#337. House Robber III
ProblemThe thief has found himself a new place for his thievery again. There is only one entrance to this area, called root.
Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night.
Given the root of the binary tree, return the maximum amount of money the thief can rob without alerting the police ...
Leetcode#105. Construct Binary Tree from Preorder and Inorder Traversal
ProblemGiven 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.
Example 1:
!https://assets.leetcode.com/uploads/2021/02/19/tree.jpg
123Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]Output: [3,9,20,null,null,15,7]
Example 2:
123Input: preorder = [-1], inorder = [-1]Output: [-1]
Constraints:
1 <= preorder.length <= 3000
inorder.length == pr ...
Leetcode#530. Minimum Absolute Difference in BST
ProblemGiven 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 1:
!https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg
123Input: root = [4,2,6,1,3]Output: 1
Example 2:
!https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg
123Input: root = [1,0,48,null,null,12,49]Output: 1
Constraints:
The number of nodes in the tree is in the range [2, 10^4].
0 <= Node.val <= 10^5
Solve法一由於有有序性質、且正整 ...
Leetcode#100. Same Tree
ProblemGiven 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.
Example 1:
!https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg
123Input: p = [1,2,3], q = [1,2,3]Output: true
Example 2:
!https://assets.leetcode.com/uploads/2020/12/20/ex2.jpg
123Input: p = [1,2], q = [1,null,2]Output: false
Example 3:
!https://assets.leetcode.com/upload ...
Leetcode#117. Populating Next Right Pointers in Each Node II
leetcode116
ProblemGiven a binary tree
1234567struct 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.
Initially, all next pointers are set to NULL.
Example 1:
1234Input: root = [1,2,3,4,5,null,7]Output: [1,#,2,3,#,4,5,7,#]Explanation:Given the above binary tree (Figure A), your function should populate each next pointer to point to its next righ ...