算法之路
1 二叉搜索树的范围和
给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和。
public int rangeSumBST(TreeNode root, int low, int high) {
// if (root == null) {
// return 0;
// }
// if (root.val > high) {
// return rangeSumBST(root.left, low, high);
// }
// if (root.val < low) {
// return rangeSumBST(root.right, low, high);
// }
// return root.val + rangeSumBST(root.left, low, high) + rangeSumBST(root.right, low, high);
int sum = 0;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node == null) {
continue;
}
if (node.val > high) {
queue.offer(node.left);
} else if (node.val < low) {
queue.offer(node.right);
} else {
sum += node.val;
queue.offer(node.left);
queue.offer(node.right);
}
}
return sum;
}
2 从上到下打印二叉树 II
从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
例如: 给定二叉树: [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回其层次遍历结果:
[ [3], [9,20], [15,7] ]
Queue<TreeNode> queue = new LinkedList<>();
List<List<Integer>> res = new ArrayList<>();
if (root != null) {
queue.add(root);
}
while (!queue.isEmpty()) {
List<Integer> tmp = new ArrayList<>();
for (int i = queue.size(); i > 0; i--) {
TreeNode node = queue.poll();
tmp.add(node.val);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
res.add(tmp);
}
return res;
}
3 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例:
输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释:
1 <---
/
2 3 <---
\
5 4 <---
来源:力扣(LeetCode)
链接:leetcode-cn.com/problems/bi…
List<Integer> res = new ArrayList<>();
public List<Integer> rightSideView(TreeNode root) {
dfs(root,0);
return res;
}
private void dfs(TreeNode root, int depth){
if (root == null) {
return;
}
if(depth == res.size()){
res.add(root.val);
}
depth++;
dfs(root.right,depth);
dfs(root.left,depth);
}
}
4 平衡二叉树
输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。
private boolean result = true;
public boolean isBalanced(TreeNode root) {
maxDepth(root);
return result;
}
public int maxDepth(TreeNode root) {
if (root == null){
return 0;
}
int l = maxDepth(root.left);
int r = maxDepth(root.right);
if (Math.abs(l - r) > 1){
result = false;
}
return 1 + Math.max(l, r);
}
}
5 二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
示例 1:
输入:root = [3,9,20,null,null,15,7] 输出:2
来源:力扣(LeetCode)
链接:leetcode-cn.com/problems/mi…
if(root == null){
return 0;
}
int left = minDepth(root.left);
int right = minDepth(root.right);
return (left == 0 || right == 0) ? (left+right+1):Math.min(left,right)+1;
}
6 二叉树的最大深度
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例: 给定二叉树 [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回它的最大深度 3 。
来源:力扣(LeetCode)
链接:leetcode-cn.com/problems/ma…
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
7 二叉树的层序遍历
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
public List<List<Integer>> levelOrder(TreeNode root) {
if(root == null){
return new ArrayList<>();
}
LinkedList<TreeNode> queue=new LinkedList<TreeNode>();
List<List<Integer>> resList=new ArrayList<List<Integer>>();
queue.add(root);
while(!queue.isEmpty()){
int size = queue.size();
List<Integer> list=new ArrayList<Integer>();
for(int i = 0; i < size; i++){
TreeNode node = queue.poll();
list.add(node.val);
if (node.left !=null ){
queue.offer(node.left);
}
if (node.right !=null ){
queue.offer(node.right);
}
}
resList.add(list);
}
return resList;
}