Leetcode
104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
For example:
Given binary tree [3,9,20,null,null,15,7],
|
|
return its depth = 3.
|
|
111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
解题思路:
与上一题不同之处在于:找最小距离要注意,可能存在左右子树为空的情况,此时值就为0,但显然值是不为0的(只有当二叉树为空才为0),所以,在这里注意一下即可!
|
|
226. Invert Binary Tree
Invert a binary tree.
|
|
to
|
|
解题思路:
翻转二叉树
|
|
100. Same Tree
Given two binary trees, 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.
|
|
101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
|
|
222. Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.
|
|
110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.
|
|
112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
Example:
Given the below binary tree and sum = 22
|
|
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
|
|
404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree.
Example:
|
|
There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
|
|
257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.
Example:
|
|
All root-to-leaf paths are:
|
|
|
|
113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
Example:
Given the below binary tree and sum = 22,
|
|
return
|
|
解题思路:
深度优先搜索策略
|
|
129. Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
Example:
|
|
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Return the sum = 12 + 13 = 25.
|
|
437. Path Sum III
ou are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
Example:
|
|
|
|
235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
Example:
|
|
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
|
|
98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).
Example:
|
|
|
|
|
|
450. Delete Node in a BST
Given 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.
Note: Time complexity should be O(height of tree).
Example:
|
|
108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
Example:
|
|
|
|
230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.
|
|
236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
|
|
Example:
the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
|