算法笔记|Day11二叉树
- ☆☆☆☆☆leetcode 144.二叉树的前序遍历
- ☆☆☆☆☆leetcode 94.二叉树的中序遍历
- ☆☆☆☆☆leetcode 145.二叉树的后序遍历
- ☆☆☆☆☆leetcode 102.二叉树的层序遍历
- ☆☆☆☆☆leetcode 107.二叉树的层次遍历II
- ☆☆☆☆☆leetcode 199.二叉树的右视图
- ☆☆☆☆☆leetcode 637.二叉树的层平均值
- ☆☆☆☆☆leetcode 429.N叉树的层序遍历
- ☆☆☆☆☆leetcode 515.在每个树行中找最大值
- ☆☆☆☆☆leetcode 116.填充每个节点的下一个右侧节点指针
- ☆☆☆☆☆leetcode 117.填充每个节点的下一个右侧节点指针II
- ☆☆☆☆☆leetcode 104.二叉树的最大深度
- ☆☆☆☆☆leetcode 111.二叉树的最小深度
☆☆☆☆☆leetcode 144.二叉树的前序遍历
题目分析
前序遍历是深度优先遍历,分别采用递归方法和迭代方法对二叉树遍历,为了前序、中序、后序三种遍历迭代方法代码逻辑相同增添统一迭代方法。
代码
1.递归遍历
/**
* 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> preorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<>();
preorder(root,res);
return res;
}
public void preorder(TreeNode root,List<Integer> res){
if(root==null)
return;
res.add(root.val);
preorder(root.left,res);
preorder(root.right,res);
}
}
2.迭代遍历
/**
* 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> preorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<>();
if(root==null)
return res;
Stack<TreeNode> stack=new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node=stack.pop();
res.add(node.val);
if(node.right!=null)
stack.push(node.right);
if(node.left!=null)
stack.push(node.left);
}
return res;
}
}
3.统一迭代遍历
/**
* 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> preorderTraversal(TreeNode root) {
List<Integer>res=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
if(root!=null)
stack.push(root);
while(!stack.isEmpty()){
TreeNode node=stack.peek();
if(node!=null){
stack.pop();
if(node.right!=null)
stack.push(node.right);
if(node.left!=null)
stack.push(node.left);
stack.push(node);
stack.push(null);
}else{
stack.pop();
node=stack.peek();
stack.pop();
res.add(node.val);
}
}
return res;
}
}
☆☆☆☆☆leetcode 94.二叉树的中序遍历
题目链接:leetcode 94.二叉树的中序遍历
题目分析
中序遍历是深度优先遍历,分别采用递归方法和迭代方法对二叉树遍历,为了前序、中序、后序三种遍历迭代方法代码逻辑相同增添统一迭代方法。
代码
1.递归遍历
/**
* 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> inorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<>();
inorder(root,res);
return res;
}
public void inorder(TreeNode root,List<Integer> res){
if(root==null)
return;
inorder(root.left,res);
res.add(root.val);
inorder(root.right,res);
}
}
2.迭代遍历
/**
* 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> inorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<>();
if(root==null)
return res;
Stack<TreeNode> stack=new Stack<>();
TreeNode cur=root;
while(cur!=null||!stack.isEmpty()){
if(cur!=null){
stack.push(cur);
cur=cur.left;
}else{
cur=stack.pop();
res.add(cur.val);
cur=cur.right;
}
}
return res;
}
}
3.统一迭代遍历
/**
* 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> inorderTraversal(TreeNode root) {
List<Integer>res=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
if(root!=null)
stack.push(root);
while(!stack.isEmpty()){
TreeNode node=stack.peek();
if(node!=null){
stack.pop();
if(node.right!=null)
stack.push(node.right);
stack.push(node);
stack.push(null);
if(node.left!=null)
stack.push(node.left);
}else{
stack.pop();
node=stack.peek();
stack.pop();
res.add(node.val);
}
}
return res;
}
}
☆☆☆☆☆leetcode 145.二叉树的后序遍历
题目分析
后序遍历是深度优先遍历,分别采用递归方法和迭代方法对二叉树遍历,为了前序、中序、后序三种遍历迭代方法代码逻辑相同增添统一迭代方法。
代码
1.递归遍历
/**
* 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> postorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<>();
postorder(root,res);
return res;
}
public void postorder(TreeNode root,List<Integer> res){
if(root==null)
return;
postorder(root.left,res);
postorder(root.right,res);
res.add(root.val);
}
}
2.迭代遍历
/**
* 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> postorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<>();
if(root==null)
return res;
Stack<TreeNode> stack=new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node=stack.pop();
res.add(node.val);
if(node.left!=null)
stack.push(node.left);
if(node.right!=null)
stack.push(node.right);
}
Collections.reverse(res);
return res;
}
}
3.统一迭代遍历
/**
* 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> postorderTraversal(TreeNode root) {
List<Integer>res=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
if(root!=null)
stack.push(root);
while(!stack.isEmpty()){
TreeNode node=stack.peek();
if(node!=null){
stack.pop();
stack.push(node);
stack.push(null);
if(node.right!=null)
stack.push(node.right);
if(node.left!=null)
stack.push(node.left);
}else{
stack.pop();
node=stack.peek();
stack.pop();
res.add(node.val);
}
}
return res;
}
}
☆☆☆☆☆leetcode 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<>();
Deque<TreeNode> deque=new LinkedList<>();
if(root==null)
return res;
deque.add(root);
while(!deque.isEmpty()){
List<Integer> itemList=new ArrayList<>();
int len=deque.size();
while(len>0){
TreeNode tempNode=deque.poll();
itemList.add(tempNode.val);
if(tempNode.left!=null)
deque.add(tempNode.left);
if(tempNode.right!=null)
deque.add(tempNode.right);
len--;
}
res.add(itemList);
}
return res;
}
}
☆☆☆☆☆leetcode 107.二叉树的层次遍历II
题目分析
需要将结果res数组反转一下,相当于将后面的结果加到list表头。
代码
/**
* 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<>();
Deque<TreeNode> deque=new LinkedList<>();
if(root==null)
return res;
deque.add(root);
while(!deque.isEmpty()){
List<Integer> itemList=new ArrayList<>();
int len=deque.size();
while(len>0){
TreeNode tempNode=deque.poll();
itemList.add(tempNode.val);
if(tempNode.left!=null)
deque.add(tempNode.left);
if(tempNode.right!=null)
deque.add(tempNode.right);
len--;
}
res.add(0,itemList);
}
return res;
}
}
☆☆☆☆☆leetcode 199.二叉树的右视图
题目链接:leetcode 199.二叉树的右视图
题目分析
层序遍历时,判断是否遍历到单层的最后面的元素,若是放进res数组中。
代码
/**
* 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<>();
Deque<TreeNode> deque=new LinkedList<>();
if(root==null)
return res;
deque.add(root);
while(!deque.isEmpty()){
List<Integer> itemList=new ArrayList<>();
int len=deque.size();
for(int i=0;i<len;i++){
TreeNode tempNode=deque.poll();
if(tempNode.left!=null)
deque.add(tempNode.left);
if(tempNode.right!=null)
deque.add(tempNode.right);
if(i==len-1)
res.add(tempNode.val);
}
}
return res;
}
}
☆☆☆☆☆leetcode 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<>();
Deque<TreeNode> deque=new LinkedList<>();
if(root==null)
return res;
deque.add(root);
while(!deque.isEmpty()){
int len=deque.size();
double sum=0;
for(int i=0;i<len;i++){
TreeNode tempNode=deque.poll();
if(tempNode.left!=null)
deque.add(tempNode.left);
if(tempNode.right!=null)
deque.add(tempNode.right);
sum+=tempNode.val;
}
res.add(sum/len);
}
return res;
}
}
☆☆☆☆☆leetcode 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<>();
Deque<Node> deque=new LinkedList<>();
if(root==null)
return res;
deque.add(root);
while(!deque.isEmpty()){
List<Integer> itemList=new ArrayList<>();
int len=deque.size();
for(int i=0;i<len;i++){
Node tempNode=deque.poll();
itemList.add(tempNode.val);
List<Node> children=tempNode.children;
if(children==null||children.size()==0)
continue;
for(Node child:children)
if(child!=null)
deque.add(child);
}
res.add(itemList);
}
return res;
}
}
☆☆☆☆☆leetcode 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<>();
Deque<TreeNode> deque=new LinkedList<>();
if(root==null)
return res;
deque.add(root);
while(!deque.isEmpty()){
int max=Integer.MIN_VALUE;
int len=deque.size();
for(int i=0;i<len;i++){
TreeNode tempNode=deque.poll();
if(tempNode.left!=null)
deque.add(tempNode.left);
if(tempNode.right!=null)
deque.add(tempNode.right);
max=Math.max(max,tempNode.val);
}
res.add(max);
}
return res;
}
}
☆☆☆☆☆leetcode 116.填充每个节点的下一个右侧节点指针
题目链接:leetcode 116.填充每个节点的下一个右侧节点指针
题目分析
层序遍历时,让前一个节点指向本节点。
代码
/*
// 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) {
Deque<Node> deque=new LinkedList<>();
if(root!=null)
deque.add(root);
while(!deque.isEmpty()){
int len=deque.size();
Node cur=deque.poll();
if(cur.left!=null)
deque.add(cur.left);
if(cur.right!=null)
deque.add(cur.right);
for(int i=1;i<len;i++){
Node next=deque.poll();
if(next.left!=null)
deque.add(next.left);
if(next.right!=null)
deque.add(next.right);
cur.next=next;
cur=next;
}
}
return root;
}
}
☆☆☆☆☆leetcode 117.填充每个节点的下一个右侧节点指针II
题目链接:leetcode 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) {
Deque<Node> deque=new LinkedList<>();
if(root!=null)
deque.add(root);
while(!deque.isEmpty()){
int len=deque.size();
Node node=null;
Node nodePre=null;
for(int i=0;i<len;i++){
if(i==0){
nodePre=deque.poll();
node=nodePre;
}else{
node=deque.poll();
nodePre.next=node;
nodePre=nodePre.next;
}
if(node.left!=null)
deque.add(node.left);
if(node.right!=null)
deque.add(node.right);
}
}
return root;
}
}
☆☆☆☆☆leetcode 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) {
List<List<Integer>> res=new ArrayList<>();
Deque<TreeNode> deque=new LinkedList<>();
if(root==null)
return 0;
deque.add(root);
while(!deque.isEmpty()){
List<Integer> itemList=new ArrayList<>();
int len=deque.size();
while(len>0){
TreeNode tempNode=deque.poll();
itemList.add(tempNode.val);
if(tempNode.left!=null)
deque.add(tempNode.left);
if(tempNode.right!=null)
deque.add(tempNode.right);
len--;
}
res.add(itemList);
}
return res.size();
}
}
☆☆☆☆☆leetcode 111.二叉树的最小深度
题目分析
层序遍历,当某个节点的左右子节点均为null,返回最小深度。
代码
/**
* 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) {
List<List<Integer>> res=new ArrayList<>();
Deque<TreeNode> deque=new LinkedList<>();
if(root==null)
return 0;
deque.add(root);
int depth=0;
while(!deque.isEmpty()){
List<Integer> itemList=new ArrayList<>();
int len=deque.size();
depth++;
while(len>0){
TreeNode tempNode=deque.poll();
itemList.add(tempNode.val);
if(tempNode.left==null&&tempNode.right==null)
return depth;
if(tempNode.left!=null)
deque.add(tempNode.left);
if(tempNode.right!=null)
deque.add(tempNode.right);
len--;
}
res.add(itemList);
}
return depth;
}
}
标签:deque,right,TreeNode,val,int,算法,二叉树,left,Day11
From: https://blog.csdn.net/m0_57632621/article/details/140781022