酒店网站源码,wordpress后台编辑框 自定义按钮,wordpress 安卓教程 pdf,峰聘网360建筑网文章目录 Leetcode 104. 二叉树的最大深度解题思路代码总结 Leetcode 111. 二叉树的最小深度解题思路代码总结 Leetcode 222.完全二叉树的节点个数解题思路代码总结 草稿图网站 java的Deque
Leetcode 104. 二叉树的最大深度 题目#xff1a;104. 二叉树的最大深度 解析#… 文章目录 Leetcode 104. 二叉树的最大深度解题思路代码总结 Leetcode 111. 二叉树的最小深度解题思路代码总结 Leetcode 222.完全二叉树的节点个数解题思路代码总结 草稿图网站 java的Deque
Leetcode 104. 二叉树的最大深度 题目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 leftDepth maxDepth(root.left);int rightDepth maxDepth(root.right);return Math.max(leftDepth, rightDepth) 1;}
}//队列但是时间很慢
class Solution {public int maxDepth(TreeNode root) {int depth 0;if (root null)return depth;QueueTreeNode queue new LinkedListTreeNode();queue.add(root);while (!queue.isEmpty()) {int size queue.size();depth;for (int i 0; i size; i) {TreeNode node queue.poll();if (node.left ! null) queue.add(node.left);if (node.right ! null) queue.add(node.right);}}return depth;}
}//栈但是时间很慢
总结
递归没写过
Leetcode 111. 二叉树的最小深度 题目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;int depth 0;QueueTreeNode queue new LinkedListTreeNode();queue.add(root);while(!queue.isEmpty()){int size queue.size();depth;for (int i 0; i size; i){TreeNode node queue.poll();if (node.left null node.right null)return depth;if (node.left ! null) queue.add(node.left);if (node.right ! null) queue.add(node.right);}}return depth;}
}//递归
/*** 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;
// if (root.left null root.right null)
// return 1;int leftDepth minDepth(root.left);int rightDepth minDepth(root.right);leftDepth leftDepth 0 ? rightDepth : leftDepth;rightDepth rightDepth 0 ? leftDepth : rightDepth;return Math.min(leftDepth, rightDepth) 1;}
}总结
暂无
Leetcode 222.完全二叉树的节点个数 题目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 leftNum countNodes(root.left);int rightNum countNodes(root.right);return 1 leftNum rightNum;}
}//遍历
class Solution {public int countNodes(TreeNode root) {int count 0;if (root null)return count;QueueTreeNode queue new LinkedListTreeNode();queue.add(root);while (!queue.isEmpty()) {int size queue.size();for (int i 0; i size; i) {TreeNode node queue.poll();count;if (node.left ! null) queue.add(node.left);if (node.right ! null) queue.add(node.right);}}return count;}
}总结
暂无