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

代码随想录算法训练营Day11

144.二叉树的前序遍历

递归法

/*** 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);}
}

迭代法

/*** 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> result=new ArrayList<>();if(root==null){return result;}  Stack<TreeNode> mystack=new Stack<>();mystack.push(root);while(!mystack.isEmpty()){TreeNode cur=mystack.pop();result.add(cur.val);if(cur.right!=null){mystack.push(cur.right);}if(cur.left!=null){mystack.push(cur.left);}}return result;}
}

94.二叉树的中序遍历

递归法

/*** 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> result=new ArrayList<>();midorder(root,result);return result;}public void midorder(TreeNode root,List<Integer> result){if(root==null){return;}midorder(root.left,result);result.add(root.val);midorder(root.right,result);}
}

迭代法

/*** 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> result=new ArrayList<>();if(root==null){return result;}Stack<TreeNode> mystack=new Stack<>();TreeNode cur=root;while(cur!=null||!mystack.isEmpty()){        if(cur!=null){mystack.push(cur);cur=cur.left;}else{cur=mystack.pop();result.add(cur.val);cur=cur.right;}}return result;}
}

145.二叉树的后序遍历

递归法

/*** 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> result=new ArrayList<>();afterorder(root,result);return result;}public void afterorder(TreeNode root,List<Integer> result){if(root==null){return;}afterorder(root.left,result);afterorder(root.right,result);result.add(root.val);}
}

迭代法

在前序遍历迭代法基础上,改变左右子树节点的入栈顺序,同时翻转输出集合

/*** 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> result=new ArrayList<>();if(root==null){return result;}Stack<TreeNode> mystack=new Stack<>();mystack.push(root);while(!mystack.isEmpty()){TreeNode cur=mystack.pop();result.add(cur.val);if(cur.left!=null){mystack.push(cur.left);}if(cur.right!=null){mystack.push(cur.right);}}Collections.reverse(result);return result;}
}

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>> result=new ArrayList<List<Integer>>();lo(root,result);return result;}public void lo(TreeNode root,List<List<Integer>> result){if(root==null)return;Deque<TreeNode> myque=new LinkedList<>();myque.offer(root);while(!myque.isEmpty()){List<Integer> curlist=new ArrayList<>();int len=myque.size();while(len>0){TreeNode cur=myque.poll();curlist.add(cur.val);len--;if(cur.left!=null){myque.offer(cur.left);}if(cur.right!=null){myque.offer(cur.right);}} result.add(curlist);      }}
}


http://www.mrgr.cn/news/34563.html

相关文章:

  • kafka消费数据太慢了,给优化下
  • Python自动化运维DevSecOps与安全自动化
  • 【MySQL】MySQL函数之JSON_EXTRACT
  • WordPress 2024主题实例镜像
  • 容器技术在持续集成与持续交付中的应用
  • PET-文件包含-FINISHED
  • JDK7u21 HashMap版
  • C++之STL—vector容器进阶篇
  • Spring源码学习:SpringMVC(2)DispatcherServlet初始化【子容器9大组件】
  • go解决引入私有包报错“Repository owner does not exist“的两种方式
  • 难题妙解——前K个高频单词
  • Vue从入门到精通:全方位掌握Vue.js开发技能
  • CF 461 B Appleman and Tree 题解(树形 dp+排列组合)
  • MySQL和SQL的区别简单了解和分析使用以及个人总结
  • 手写数字识别案例分析(torch,深度学习入门)
  • 看Threejs好玩示例,学习创新与技术(React-three-fiber)
  • 有空格输入
  • Java设计模式——工厂模式扩展
  • Vue3(二)计算属性Computed,监视属性watch,watchEffect,标签的ref属性,propos属性,生命周期,自定义hook
  • gtk安装和测试
  • 半导体芯闻--20240923
  • Vue使用Vue Router路由:通过URL传递与获取参数
  • excel怎么转换json
  • Java刷题知识总结(一)
  • mapty项目架构
  • 【链表操作】前驱和后继