下面所记录的是我在刷 LeetCode Array 相关的题目自己的解法。
100. Same Tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value
JavaScript Solution
1 | /** |
Java Solution
1 | /** |
101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1 | 1 |
But the following [1,2,2,null,3,null,3]
is not:
1 | 1 |
Note:
Bonus points if you could solve it both recursively and iteratively.
JavaScript Solution
使用的是递归。
1 | /** |
Java Solution
使用的是循环,利用 Stack 来存储要比较的节点
1 | public boolean isSymmetric(TreeNode root) { |
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.
JavaScript Solution
深度优先搜索算法:
1 | /** |
Java Solution
广度优先搜索算法。
1 | public int maxDepth(TreeNode root) { |
107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
1 | 3 |
return its bottom-up level order traversal as:
1 | [ |
JavaScript Solution
使用广度优先遍历该树
1 | /** |
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.
JavaScript Solution
通过一个排好序的数组,生成平衡二叉查找树。
1 | /** |
110. Balanced Binary Tree
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 depth of the two subtrees of every node never differ by more than 1.
JavaScript Solution
1.The first method checks whether the tree is balanced strictly according to the definition of balanced binary tree: the difference between the heights of the two sub trees are not bigger than 1, and both the left sub tree and right sub tree are also balanced. With the helper function depth(), we could easily write the code;
1 | /** |
For the current node root, calling depth() for its left and right children actually has to access all of its children, thus the complexity is O(N). We do this for each node in the tree, so the overall complexity of isBalanced will be O(N^2). This is a top down approach.
Java Solution
2.The second method is based on DFS. Instead of calling depth() explicitly for each child node, we return the height of the current node in DFS recursion. When the sub tree of the current node (inclusive) is balanced, the function dfsHeight() returns a non-negative value as the height. Otherwise -1 is returned. According to the leftHeight and rightHeight of the two children, the parent node could check if the sub tree is balanced, and decides its return value.
1 | /** |
In this bottom up approach, each node in the tree only need to be accessed once. Thus the time complexity is O(N), better than the first solution.
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.
JavaScript Solution
1 | /** |
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.
For example:
Given the below binary tree and sum = 22
,
1 | 5 |
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
JavaScript Solution
1 | /** |
Best Solution
最优解和我的想法大致相同,但是在递归函数的参数设计上比我的要好。
1 | public class Solution { |
226. Invert Binary Tree
Invert a binary tree.
1 | 4 |
to
1 | 4 |
JavaScript Solution
DFS
The below solution is correct, but it is also bound to the application stack, which means that it’s no so much scalable - (you can find the problem size that will overflow the stack and crash your application), so more robust solution would be to use stack data structure.
大意是,因为使用递归,并且没有使用尾递归,所以会导致栈溢出,最好不要这样写。可以使用队列,进行广度优先搜索。
1 | /** |
Java Solution
BFS
1 | /** |
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.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
1 | _______6______ |
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.
JavaScript Solution
递归:
1 | /** |
Best Solution
非递归
1 | /** |
257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1 | 1 |
All root-to-leaf paths are:
1 | ["1->2->5", "1->3"] |
JavaScript Solution
1 | /** |
Best Solution
最优解在递归的参数上进行 path 的修改,比我的解法上减少了更多的初始条件判断。
1 | public List<String> binaryTreePaths(TreeNode root) { |
404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree.
Example:
1 | 3 |
JavaScript Solution
Iterative method.
1 | /** |
Java Solution
Recursive method
1 | public int sumOfLeftLeaves(TreeNode root) { |
437. Path Sum III
You 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:
1 | root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 |
JavaScript Solution
做法就是,遍历树,并且计算所有中间和的结果,放在 map 里面。
1 | /** |
Best Solution
1 | public int pathSum(TreeNode root, int sum) { |
501. Find Mode in Binary Search Tree
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.
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.
- The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
- Both the left and right subtrees must also be binary search trees.
For example:
Given BST [1,null,2,2]
,
1 | 1 |
return [2]
.
Note: If a tree has more than one mode, you can return them in any order.
Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
Best Solution
1 | /** |