[二叉树]相关题目
树
树的遍历方式
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
层序遍历:
https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/
二叉树的属性
https://leetcode-cn.com/problems/same-tree/
https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
public int minDepth(TreeNode root) {
return process(root);
}
public int process(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
//全局最小深度
int min = Integer.MAX_VALUE;
//左子树的深度
if (root.left != null){
int leftDepth = process(root.left);
min = Math.min(leftDepth, min);
}
//右子树的深度
if (root.right != null) {
int rightDepth = process(root.right);
min = Math.min(rightDepth, min);
}
return min + 1;
}
public int process2(TreeNode root) {
if (root == null) {
return 0;
}
// 情况1:左右子树均为null,到达叶子节点
if (root.left == null && root.right == null) {
return 1;
}
int leftDepth = process(root.left);
int rightDepth = process(root.right);
// 情况2:左子树或右子树有一个为null,返回较大的深度
if (roo.left == null || root.right == null) {
return Math.max(leftDepth, rightDepth) + 1;
}
// 情况3:左子树和右子树都不为null,返回较小的深度
return Math.min(leftDepth, rightDepth) + 1;
}
https://leetcode-cn.com/problems/count-complete-tree-nodes/
题解:
https://leetcode-cn.com/problems/balanced-binary-tree/
https://leetcode-cn.com/problems/binary-tree-paths/
https://leetcode-cn.com/problems/path-sum/
https://leetcode-cn.com/problems/path-sum-ii/
https://leetcode-cn.com/problems/path-sum-iii/
https://leetcode-cn.com/problems/path-sum-iv/
https://leetcode-cn.com/problems/maximum-width-of-binary-tree/