本文主要是介绍【算法刷题day15】Leetcode:各种层序遍历、226.翻转二叉树、101. 对称二叉树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- Leetcode 层序遍历
- 解题思路
- 代码
- 总结
- Leetcode 226.翻转二叉树
- 解题思路
- 代码
- 总结
- Leetcode 101. 对称二叉树
- 解题思路
- 代码
- 总结
草稿图网站
java的Deque
Leetcode 层序遍历
题目:102.二叉树的层序遍历
题目:107. 二叉树的层序遍历 II
题目:199.二叉树的右视图
题目:637.二叉树的层平均值
题目:429.N叉树的层序遍历
题目:515.在每个树行中找最大值
题目:116.填充每个节点的下一个右侧节点指针
题目:117.填充每个节点的下一个右侧节点指针II
题目:104.二叉树的最大深度
解析:代码随想录解析
解题思路
代码
102.二叉树的层序遍历
/*** 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 List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> res = new ArrayList<List<Integer>>();if (root == null)return res;Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.add(root);while(!queue.isEmpty()){int size = queue.size();List<Integer> item = new ArrayList<Integer>();for(int i = 0; i < size; i++){TreeNode node = queue.poll();item.add(node.val);if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}res.add(item);}return res;}
}
107. 二叉树的层序遍历 II
/*** 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 List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> res = new ArrayList<List<Integer>>();if (root == null)return res;Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.add(root);while(!queue.isEmpty()){int size = queue.size();List<Integer> item = new ArrayList<Integer>();for (int i = 0; i < size; i++){TreeNode node = queue.poll();item.add(node.val);if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}res.add(item);}Collections.reverse(res);return res;}
}
199.二叉树的右视图
/*** 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 List<Integer> rightSideView(TreeNode root) {List<Integer> res = new ArrayList<Integer>();if (root == null)return res;Deque<TreeNode> deque = new LinkedList<TreeNode>();deque.add(root);res.add(root.val);while (!deque.isEmpty()){int size = deque.size();for (int i = 0; i < size; i++){TreeNode node = deque.poll();if (node.left != null) deque.add(node.left);if (node.right != null) deque.add(node.right);}if (!deque.isEmpty())res.add(deque.getLast().val);}return res;}
}
637.二叉树的层平均值
/*** 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 List<Double> averageOfLevels(TreeNode root) {List<Double> res = new ArrayList<Double>();if (root == null)return res;Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.add(root);while (!queue.isEmpty()){int size = queue.size();double sum = 0;for (int i = 0; i < size; i++){TreeNode node = queue.poll();sum += node.val;if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}res.add(sum / size);}return res;}
}
429.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 List<List<Integer>> levelOrder(Node root) {List<List<Integer>> res = new ArrayList<List<Integer>>();if (root == null)return res;Queue<Node> queue = new LinkedList<Node>();queue.add(root);while (!queue.isEmpty()){int size = queue.size();List<Integer> item = new ArrayList<Integer>();for (int i = 0; i < size; i++){Node node = queue.poll();item.add(node.val);for (Node n : node.children){if (n != null)queue.add(n);}}res.add(item);}return res;}
}
515.在每个树行中找最大值
/*** 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 List<Integer> largestValues(TreeNode root) {List<Integer> res = new ArrayList<Integer>();if (root == null)return res;Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.add(root);while (!queue.isEmpty()){int size = queue.size();int max = Integer.MIN_VALUE;for (int i = 0; i < size; i++){TreeNode node = queue.poll();max = Math.max(max, node.val);if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}res.add(max);}return res;}
}
116.填充每个节点的下一个右侧节点指针
117.填充每个节点的下一个右侧节点指针II
/*
// Definition for a Node.
class Node {public int val;public Node left;public Node right;public Node next;public Node() {}public Node(int _val) {val = _val;}public Node(int _val, Node _left, Node _right, Node _next) {val = _val;left = _left;right = _right;next = _next;}
};
*/class Solution {public Node connect(Node root) {if (root == null)return root;Queue<Node> queue = new LinkedList<Node>();queue.add(root);while (!queue.isEmpty()){int size = queue.size();Node pre = queue.poll();if (pre.left != null) queue.add(pre.left);if (pre.right != null) queue.add(pre.right);for (int i = 1; i < size; i++){Node cur = queue.poll();if (cur.left != null) queue.add(cur.left);if (cur.right != null) queue.add(cur.right);pre.next = cur;pre = cur;}}return root;}
}
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) {int depth = 0;if (root == null)return depth;Queue<TreeNode> queue = new LinkedList<TreeNode>();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;}
}
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 minDepth = Integer.MAX_VALUE;int depth = 0;Queue<TreeNode> queue = new LinkedList<TreeNode>();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)minDepth = Math.min(minDepth, depth);if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}}return minDepth==Integer.MAX_VALUE ? 0 : minDepth;}
}//优化,不需要全部遍历完,因为是一层一层找下去,所以第一个叶子就是最低的
/*** 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;Queue<TreeNode> queue = new LinkedList<TreeNode>();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;}
}
总结
暂无
Leetcode 226.翻转二叉树
题目:226.翻转二叉树
解析:代码随想录解析
解题思路
使用局部遍历存储交换后的,然后稀里糊涂就过了。
代码
/*** 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 TreeNode invertTree(TreeNode root) {if (root == null)return root;TreeNode tmp = invertTree(root.left);root.left = invertTree(root.right);root.right = tmp;return root;}
}//更能体现前序遍历和后续遍历
class Solution {public TreeNode invertTree(TreeNode root) {if (root == null)return root;invertTree(root.left);invertTree(root.right);swapChildren(root);return root;}private void swapChildren(TreeNode node){TreeNode tmp = node.left;node.left = node.right;node.right = tmp;}
}//层序遍历
class Solution {public TreeNode invertTree(TreeNode root) {if (root == null)return root;Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.add(root);while (!queue.isEmpty()){int size = queue.size();for (int i = 0; i < size; i++){TreeNode node = queue.poll();swapChildren(node);if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}}return root;}private void swapChildren(TreeNode node){TreeNode tmp = node.left;node.left = node.right;node.right = tmp;}
}//前序
class Solution {public TreeNode invertTree(TreeNode root) {if (root == null)return root;Stack<TreeNode> stack = new Stack<TreeNode>();stack.push(root);while(!stack.isEmpty()){TreeNode node = stack.pop();swapChildren(node);if (node.left != null) stack.push(node.left);if (node.right != null) stack.push(node.right);}return root;}private void swapChildren(TreeNode node){TreeNode tmp = node.left;node.left = node.right;node.right = tmp;}
}//统一深度遍历
class Solution {public TreeNode invertTree(TreeNode root) {if (root == null)return root;Stack<TreeNode> stack = new Stack<TreeNode>();stack.push(root);while(!stack.isEmpty()){TreeNode node = stack.peek();if (node != null){stack.pop();if (node.left != null) stack.push(node.left);if (node.right != null) stack.push(node.right);stack.push(node);stack.push(null);}else{stack.pop();node = stack.pop();swapChildren(node);}}return root;}private void swapChildren(TreeNode node){TreeNode tmp = node.left;node.left = node.right;node.right = tmp;}
}
总结
暂无
Leetcode 101. 对称二叉树
题目:101. 对称二叉树
解析:代码随想录解析
解题思路
遍历遍历遍历
代码
//递归
/*** 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 boolean isSymmetric(TreeNode root) {if (root == null)return true;return compare(root.left, root.right);}public boolean compare(TreeNode left, TreeNode right){if (left == null && right == null)return true;else if (left == null || right == null || left.val != right.val)return false;elsereturn compare(left.left, right.right) && compare(left.right, right.left);}
}//使用队列
class Solution {public boolean isSymmetric(TreeNode root) {if (root == null)return true;Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.add(root.left);queue.add(root.right);while (!queue.isEmpty()){TreeNode left = queue.poll();TreeNode right = queue.poll();if (left == null && right == null)continue;if (left == null || right == null || left.val != right.val)return false;queue.add(left.left);queue.add(right.right);queue.add(left.right);queue.add(right.left);}return true;}
}//使用栈
class Solution {public boolean isSymmetric(TreeNode root) {if (root == null)return true;Stack<TreeNode> stack = new Stack<TreeNode>();stack.push(root.left);stack.push(root.right);while (!stack.isEmpty()){TreeNode left = stack.pop();TreeNode right = stack.pop();if (left == null && right == null)continue;if (left == null || right == null || left.val != right.val)return false;stack.push(left.left);stack.push(right.right);stack.push(left.right);stack.push(right.left);}return true;}
}
总结
暂无
这篇关于【算法刷题day15】Leetcode:各种层序遍历、226.翻转二叉树、101. 对称二叉树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!