当前位置: 首页 > wzjs >正文

鞍山网站建设找金航娄底市住房和城乡建设局官方网站

鞍山网站建设找金航,娄底市住房和城乡建设局官方网站,北京网站制作方案公司,c 做网站实例LeetCode/卡码网题目: 144. 二叉树的前序遍历94. 二叉树的中序遍历145. 二叉树的后序遍历102. 二叉树的层序遍历107.二叉树的层次遍历II199. 二叉树的右视图637. 二叉树的层平均值429. N 叉树的层序遍历515. 在每个树行中找最大值116. 填充每个节点的下一个右侧节点指针117. 填…

LeetCode/卡码网题目:

  • 144. 二叉树的前序遍历
  • 94. 二叉树的中序遍历
  • 145. 二叉树的后序遍历
  • 102. 二叉树的层序遍历
  • 107.二叉树的层次遍历II
  • 199. 二叉树的右视图
  • 637. 二叉树的层平均值
  • 429. N 叉树的层序遍历
  • 515. 在每个树行中找最大值
  • 116. 填充每个节点的下一个右侧节点指针
  • 117. 填充每个节点的下一个右侧节点指针 II
  • 104. 二叉树的最大深度
  • 111. 二叉树的最小深度

其他:

今日总结
往期打卡


144. 二叉树的前序遍历

跳转:144. 二叉树的前序遍历

问题:

在这里插入图片描述

思路:

递归,迭代,空指针标记模拟递归

代码(递归):

void handle(TreeNode root,List<Integer> list){if(root == null) return;list.add(root.val);handle(root.left,list); handle(root.right,list);}public List<Integer> inorderTraversal(TreeNode root) {List<Integer> ans = new ArrayList<>();handle(root,ans);return ans;}

代码(迭代):

class Solution {public List<Integer> preorderTraversal(TreeNode root) {if(root==null) return new ArrayList<>();Stack<TreeNode> stack = new Stack<>();stack.push(root);List<Integer> ans = new ArrayList<>();while(!stack.isEmpty()){TreeNode tmp = stack.pop();ans.add(tmp.val);if(tmp.right!=null){stack.push(tmp.right);}if(tmp.left!=null){stack.push(tmp.left);} }return ans;}
}

代码(空指针标记模拟递归):

public List<Integer> inorderTraversal(TreeNode root) {List<Integer> ans = new ArrayList<>();if(root==null) return ans;Stack<TreeNode> stack = new Stack<>();stack.push(root);while(!stack.isEmpty()){TreeNode tmp = stack.pop();if(tmp==null){tmp = stack.pop();ans.add(tmp.val);}else{if(tmp.right!=null) stack.add(tmp.right);if(tmp.left!=null) stack.add(tmp.left);stack.add(tmp);stack.add(null);}}return ans;}

94. 二叉树的中序遍历

跳转: 94. 二叉树的中序遍历

在这里插入图片描述

问题:

思路:

递归,迭代,空指针标记模拟递归

代码(递归):

void handle(TreeNode root,List<Integer> list){if(root == null) return;handle(root.left,list);list.add(root.val);handle(root.right,list);}public List<Integer> inorderTraversal(TreeNode root) {List<Integer> ans = new ArrayList<>();handle(root,ans);return ans;}

代码(迭代):

class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> ans = new ArrayList<>();if(root==null) return ans;Stack<TreeNode> stack = new Stack<>();while(root!=null||!stack.isEmpty()){if(root!=null){stack.push(root);root = root.left;}else{root = stack.pop();ans.add(root.val);root = root.right;}}return ans;}
}

代码(空指针标记模拟递归):

public List<Integer> inorderTraversal(TreeNode root) {List<Integer> ans = new ArrayList<>();if(root==null) return ans;Stack<TreeNode> stack = new Stack<>();stack.push(root);while(!stack.isEmpty()){TreeNode tmp = stack.pop();if(tmp==null){tmp = stack.pop();ans.add(tmp.val);}else{if(tmp.right!=null) stack.add(tmp.right);stack.add(tmp);stack.add(null);if(tmp.left!=null) stack.add(tmp.left);}}return ans;}

145. 二叉树的后序遍历

跳转: 145. 二叉树的后序遍历

问题:

在这里插入图片描述

思路:

递归,迭代,空指针标记模拟递归

代码(递归):

void handle(TreeNode root,List<Integer> list){if(root == null) return;handle(root.left,list);handle(root.right,list);list.add(root.val);}public List<Integer> inorderTraversal(TreeNode root) {List<Integer> ans = new ArrayList<>();handle(root,ans);return ans;}

代码(迭代):

    public List<Integer> postorderTraversal(TreeNode root) {List<Integer> ans = new ArrayList<>();if (root == null)return ans;Stack<TreeNode> stack = new Stack<>();stack.push(root);while(!stack.isEmpty()){TreeNode tmp = stack.pop();ans.add(tmp.val);if(tmp.left!=null) stack.add(tmp.left);if(tmp.right!=null) stack.add(tmp.right);}Collections.reverse(ans);return ans;}

代码(空指针标记模拟递归):

    public List<Integer> postorderTraversal(TreeNode root) {List<Integer> ans = new ArrayList<>();if (root == null)return ans;Stack<TreeNode> stack = new Stack<>();stack.push(root);while(!stack.isEmpty()){TreeNode tmp = stack.pop();if(tmp==null){tmp = stack.pop();ans.add(tmp.val);}else{stack.push(tmp);stack.push(null);if(tmp.right!=null) stack.push(tmp.right);if(tmp.left!=null) stack.push(tmp.left);}}return ans;}

102. 二叉树的层序遍历

跳转: 102. 二叉树的层序遍历

问题

在这里插入图片描述

思路:

利用队列层序遍历

代码:

class Solution {public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> ans = new ArrayList<>();if(root==null) return ans;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){List<Integer> list = new ArrayList<>();int len = queue.size();while(len-->0){TreeNode tmp = queue.poll();list.add(tmp.val);if(tmp.left!=null) queue.add(tmp.left);if(tmp.right!=null) queue.add(tmp.right);}ans.add(list);}return ans;}
}

107.二叉树的层次遍历II

跳转: 107. 二叉树的层序遍历 II

问题

在这里插入图片描述

代码:

class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> ans = new ArrayList<>();if(root==null) return ans;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){List<Integer> list = new ArrayList<>();int len = queue.size();while(len-->0){TreeNode tmp = queue.poll();list.add(tmp.val);if(tmp.left!=null) queue.add(tmp.left);if(tmp.right!=null) queue.add(tmp.right);}ans.add(list);}Collections.reverse(ans);return ans;}
}

199. 二叉树的右视图

跳转: 199. 二叉树的右视图

问题

在这里插入图片描述

代码:

class Solution {public List<Integer> rightSideView(TreeNode root) {List<Integer> ans = new ArrayList<>();if(root==null) return ans;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){int right=0;int len = queue.size();for(int i=0;i<len;i++){TreeNode tmp = queue.poll();right = tmp.val;if(tmp.left!=null) queue.add(tmp.left);if(tmp.right!=null) queue.add(tmp.right);}if(len>0)ans.add(right);}return ans;}
}

637. 二叉树的层平均值

跳转: 637. 二叉树的层平均值

问题

在这里插入图片描述

代码:

class Solution {public List<Double> averageOfLevels(TreeNode root) {List<Double> ans = new ArrayList<>();if(root==null) return ans;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){Double avg=0.0;int len = queue.size();for(int i=0;i<len;i++){TreeNode tmp = queue.poll();avg += tmp.val;if(tmp.left!=null) queue.add(tmp.left);if(tmp.right!=null) queue.add(tmp.right);}if(len!=0)ans.add(avg/len);}return ans;}
}

429. N 叉树的层序遍历

跳转: 429. N 叉树的层序遍历

问题

在这里插入图片描述

代码:

class Solution {public List<List<Integer>> levelOrder(Node root) {List<List<Integer>> ans = new ArrayList<>();if(root==null) return ans;Queue<Node> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){List<Integer> list = new ArrayList<>();int len = queue.size();while(len-->0){Node tmp = queue.poll();list.add(tmp.val);for(Node child:tmp.children){queue.add(child);}}ans.add(list);}return ans;}
}

515. 在每个树行中找最大值

跳转: 515. 在每个树行中找最大值

问题

在这里插入图片描述

代码:

class Solution {public List<Integer> largestValues(TreeNode root) {List<Integer> ans = new ArrayList<>();if(root==null) return ans;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){int max=Integer.MIN_VALUE;int len = queue.size();for(int i=0;i<len;i++){TreeNode tmp = queue.poll();max = Math.max(max,tmp.val);if(tmp.left!=null) queue.add(tmp.left);if(tmp.right!=null) queue.add(tmp.right);}if(len>0)ans.add(max);}return ans;}
}

116. 填充每个节点的下一个右侧节点指针

跳转: 116. 填充每个节点的下一个右侧节点指针

问题

在这里插入图片描述

代码:

class Solution {public Node connect(Node root) {if(root==null) return null;Queue<Node> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){int len = queue.size();for(int i=0;i<len;i++){Node tmp = queue.poll();if(i==len-1) tmp.next = null;else tmp.next = queue.peek();if(tmp.left!=null) queue.add(tmp.left);if(tmp.right!=null) queue.add(tmp.right);}}return root;}
}

117. 填充每个节点的下一个右侧节点指针 II

跳转: 117. 填充每个节点的下一个右侧节点指针 II

问题

在这里插入图片描述

代码:

class Solution {public Node connect(Node root) {if(root==null) return null;Queue<Node> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){int len = queue.size();for(int i=0;i<len;i++){Node tmp = queue.poll();if(i==len-1) tmp.next = null;else tmp.next = queue.peek();if(tmp.left!=null) queue.add(tmp.left);if(tmp.right!=null) queue.add(tmp.right);}}return root;}
}

104. 二叉树的最大深度

跳转: 104. 二叉树的最大深度

问题

在这里插入图片描述

代码:

class Solution {public int maxDepth(TreeNode root) {if(root==null) return 0;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);int deep = 0;while(!queue.isEmpty()){int len = queue.size();deep ++;for(int i=0;i<len;i++){TreeNode tmp = queue.poll();if(tmp.left!=null) queue.add(tmp.left);if(tmp.right!=null) queue.add(tmp.right);}}return deep;}
}

111. 二叉树的最小深度

跳转: 111. 二叉树的最小深度

问题

在这里插入图片描述

代码:

class Solution {public int minDepth(TreeNode root) {if(root==null) return 0;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);int deep = 0;while(!queue.isEmpty()){int len = queue.size();deep ++;for(int i=0;i<len;i++){TreeNode tmp = queue.poll();if(tmp.left!=null) queue.add(tmp.left);if(tmp.right!=null) queue.add(tmp.right);if(tmp.left==null&&tmp.right==null) return deep;}}return deep;}
}

总结

练习了递归遍历和层序遍历,后面都是层序遍历的题,所以每题改动不大
为什么如此匆忙,因为电脑快没电了
建议去力扣自己练习,使用效果更佳

往期打卡

代码随想录算法训练营第一天

代码随想录算法训练营第二天

代码随想录算法训练营第三天

代码随想录算法训练营第四天

代码随想录算法训练营周末一

代码随想录算法训练营第五天

代码随想录算法训练营第六天

代码随想录算法训练营第七天

代码随想录算法训练营第八天

代码随想录算法训练营第九天

代码随想录算法训练营第十天

代码随想录算法训练营周末二

http://www.dtcms.com/wzjs/605910.html

相关文章:

  • 网站群建设费用武进区建设局网站
  • 养老网站建设团员关系没转就作废吗
  • 上海建设小学网站杭州优化排名哪家好
  • 企业网站设计哪家好网站iis安全配置
  • 医院网站怎么建设门户网站建设工作
  • 基础建设期刊在哪个网站可以查wordpress屏蔽国外ip访问
  • 江阴网站建设工作室wordpress首页文件
  • vps怎么搭建网站南阳千牛网站建设
  • 新建的网站百度搜不到wordpress知识库主题
  • 网站建设经典案例如何重装wordpress
  • dw做网站菜单栏网页版qq登录入口官网手机
  • 顺义网站优化购买网站模板
  • 湖北省建设厅网站a群整合营销传播理论
  • 新乡专业做网站多少钱广东建筑人才网
  • 赣州专业网站推广哪家好设计公司网站建设模板图
  • 为什么选用美食做网站主页北京网站制作公司公司
  • 北京网站制作工作室wordpress提交评论卡死
  • 网站修改图片怎么做网站视频转码软件
  • 样式模板网站深圳网站优化费用
  • 廊坊网站建设招聘网站备案用户名忘了怎么办
  • 在上海卖商铺做哪个网站好手机上怎么做自己卖菜的网站
  • 秒赞网站建设有质感的wordpress主题
  • 厦门免费网站建设21世纪上海人才网官网
  • wordpress建设企业网站宁波seo推广优化
  • 广州网站建设app开发百度站长工具综合查询
  • 套用网站模板企业网站导航下拉菜单怎么做
  • 武安网站建设哪些网站是用php编写的
  • 长治网站制作怎么做网站快照怎么更新
  • 为网站开发uwp应用企业网站建设后期维护费用
  • 建设flash网站四川城乡和住房建设厅网站首页