优先掌握递归的方式
104.二叉树的最大深度
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// public int maxDepth(TreeNode root) {
// if(root == null ) return 0;
// int depth = 0;
// Queue<TreeNode> queue = new LinkedList<>();
// queue.offer(root);
// while(!queue.isEmpty()){
// int len = queue.size();
// depth++;
// while(len > 0){
// TreeNode tmpNode = queue.poll();
// if(tmpNode.left != null) queue.offer(tmpNode.left);
// if(tmpNode.right != null) queue.offer(tmpNode.right);
// len--;
// }
// }
// return depth;
// }
public int maxDepth(TreeNode root) {
if(root == null ) return 0;
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return Math.max(leftDepth,rightDepth) +1;
}
}
- N 叉树的最大深度
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
// public int maxDepth(Node root) {
// if(root==null) return 0;
// Queue<Node> queue = new LinkedList<>();
// queue.offer(root);
// int depth = 0;
// while(!queue.isEmpty()){
// depth++;
// int len = queue.size();
// while(len>0){
// Node node = queue.poll();
// for(int i = 0;i<node.children.size(); i++){
// if(node.children.get(i)!= null){
// queue.offer(node.children.get(i));
// }
// }
// len--;
// }
// }
// return depth;
// }
public int maxDepth(Node root) {
if(root==null) return 0;
int depth = 0;
if(root.children != null){
for(Node child : root.children){
depth = Math.max(depth,maxDepth(child));
}
}
return depth+1;
}
}
111.二叉树的最小深度
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// public int minDepth(TreeNode root) {
// if(root== null) return 0;
// Queue<TreeNode> queue = new LinkedList<>();
// queue.offer(root);
// int depth = 0;
// while(!queue.isEmpty()){
// depth ++;
// int size = queue.size();
// for(int i = 0; i<size;i++){
// TreeNode tmpNode = queue.poll();
// if(tmpNode.left == null && tmpNode.right==null)
// return depth;
// if(tmpNode.left != null) queue.offer(tmpNode.left);
// if(tmpNode.right != null) queue.offer(tmpNode.right);
// }
// }
// return depth;
// }
public int minDepth(TreeNode root) {
if(root== null) return 0;
int leftDepth = minDepth(root.left);
int rightDepth = minDepth(root.right);
if(root.left != null && root.right == null){
return leftDepth + 1;
}
if(root.right != null && root.left == null){
return rightDepth + 1;
}
return Math.min(leftDepth,rightDepth) + 1;
}
}
222.完全二叉树的节点个数
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// public int countNodes(TreeNode root) {
// if(root == null)return 0;
// int leftCount = countNodes(root.left);
// int rightCount = countNodes(root.right);
// return leftCount + rightCount +1;
// }
public int countNodes(TreeNode root) {
if(root==null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
int res = 0;
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
while(size-- >0){
TreeNode node = queue.poll();
res++;
if(node.left != null) queue.offer(node.left);
if(node.right != null) queue.offer(node.right);
}
}
return res;
}
}