文章目录
- 二叉树的深度和高度
- 104.二叉树的最大深度
- 思路:
- **递归法**
- 迭代法-层序遍历
- 559. N 叉树的最大深度
- 思路一-迭代法
- 思路二-递归法
- 111. 二叉树的最小深度
- 思路1-迭代
- 思路2-递归
- 222.完全二叉树的节点个数
- 思路一:二叉树的递归和迭代
- 递归
- 迭代:
- 思路二:判断完全二叉树
- 如何判断是否满足完全二叉树?
- 递归三部曲
- 代码:
二叉树的深度和高度

104.二叉树的最大深度

思路:
递归法
本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。
**二叉树节点的深度:**指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
**二叉树节点的高度:**指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)
而根节点的高度就是二叉树的最大深度,所以本题中我们通过后序求的根节点高度来求的二叉树最大深度。
//递归
class Solution {public int maxDepth(TreeNode root) {if(root==null)return 0;int left=maxDepth(root.left);int right=maxDepth(root.right);int height=Math.max(left,right)+1;return height;}
}
迭代法-层序遍历
class Solution {public int maxDepth(TreeNode root) {// List<Integer>res =new ArrayList<>();Queue<TreeNode>queue =new LinkedList<>();if(root!=null){queue.add(root);}int deep=0;while(!queue.isEmpty()){deep++;int len=queue.size();for(int i=0;i<len;i++){TreeNode node = queue.poll();if(node.left!=null)queue.add(node.left);if(node.right!=null)queue.add(node.right);}}return deep;}
}
559. N 叉树的最大深度

思路一-迭代法
class Solution {public int maxDepth(Node root) {// List<Integer>res =new ArrayList<>();Queue<Node>queue =new LinkedList<>();if(root!=null){queue.add(root);}int deep=0;while(!queue.isEmpty()){deep++;int len=queue.size();for(int i=0;i<len;i++){Node node = queue.poll();// if(node.left!=null)queue.add(node.left);// if(node.right!=null)queue.add(node.right);for(Node child:node.children){if(child!=null){queue.add(child);}}}}return deep;}
}
思路二-递归法
class Solution {public int maxDepth(Node root) {// List<Integer>res =new ArrayList<>();if (root == null)return 0;int deep = 0;if (root.children != null) {for (Node child : root.children) {deep = Math.max(maxDepth(child), deep);}}return deep + 1;}
}
111. 二叉树的最小深度

思路1-迭代
遇到两个节点都为null时,直接返回深度即可
class Solution {public int minDepth(TreeNode root) {// List<Integer>res =new ArrayList<>();Queue<TreeNode>queue =new LinkedList<>();if(root!=null){queue.add(root);}int deep=0;while(!queue.isEmpty()){deep++;int len=queue.size();for(int i=0;i<len;i++){TreeNode node = queue.poll();if(node.left!=null)queue.add(node.left);if(node.right!=null)queue.add(node.right);if(node.left==null&&node.right==null){return deep;}}}return deep;}
}
思路2-递归
所以,如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。
反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。
遍历的顺序为后序(左右中),可以看出:求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。
class Solution {public int minDepth(TreeNode root) {int deep=0;if(root==null)return 0;int left=minDepth(root.left);int right=minDepth(root.right);if(root.left!=null&&root.right==null){return left+1;}if(root.right!=null&&root.left==null){return right+1;}int height=Math.min(left,right);return height+1;}
}
222.完全二叉树的节点个数

思路一:二叉树的递归和迭代
递归
class Solution {// 通用递归解法public int countNodes(TreeNode root) {if(root == null) {return 0;}return countNodes(root.left) + countNodes(root.right) + 1;}
}
迭代:
class Solution {// 迭代法public int countNodes(TreeNode root) {if (root == null) return 0;Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);int result = 0;while (!queue.isEmpty()) {int size = queue.size();while (size -- > 0) {TreeNode cur = queue.poll();result++;if (cur.left != null) queue.offer(cur.left);if (cur.right != null) queue.offer(cur.right);}}return result;}
思路二:判断完全二叉树

- 完全二叉树1

- 完全二叉树2

如何判断是否满足完全二叉树?

递归三部曲

代码:
class Solution {public int countNodes(TreeNode root) {if(root==null)return 0;TreeNode left=root.left;TreeNode right=root.right;int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便while(left!=null){//一直查找到最小的节点left=left.left;leftDepth++;}while(right!=null){right=right.right;rightDepth++;}// 代表是完全二叉树if(leftDepth == rightDepth){return (2<<leftDepth)-1;//相当于2^4-1}// int leftdepth=countNodes(root.left);// int rightdepth=countNodes(root.right);return countNodes(root.left)+countNodes(root.right)+1;}
}