嘉兴网站建设的前景网站推广公司兴田德润在哪儿
嘉兴网站建设的前景,网站推广公司兴田德润在哪儿,昆明经济技术开发区官方门户网站,苏州网站开发建设方法学习目标#xff1a;
每天复习代码随想录上的题目1-2道算法#xff08;时间充足可以继续#xff09; 今日碎碎念#xff1a;
1#xff09;今天开始是二叉树系列
2#xff09;出租屋里不知道干啥#xff0c;看看书啊刷刷算法#xff0c;打打游戏#xff0c;学学技术…学习目标
每天复习代码随想录上的题目1-2道算法时间充足可以继续 今日碎碎念
1今天开始是二叉树系列
2出租屋里不知道干啥看看书啊刷刷算法打打游戏学学技术啥的不让自己太闲着才行。
3天天都是吃外卖不出门了都后续等到9号回来之后继续开始整理八股以复习为主。 力扣刷题
算法
力扣102102. 二叉树的层序遍历
dfs做法
class Solution {//结果集public ListListInteger res new ArrayListListInteger();public ListListInteger levelOrder(TreeNode root) {dfs(root,0);return res;}//dfs方式public void dfs(TreeNode node,Integer deep){if(node null) return;//记录深度deep;//开始将遍历到的层加入大结果集if(res.size() deep){//解读该if代码块如果走到下一层了我们就需要new一个新的ListListInteger item new ArrayList();//将新一层的小结果集放入大结果集res.add(item);}//开始dfs通过deep找到对应层级的小结果集来存入遍历到的节点res.get(deep-1).add(node.val);dfs(node.left,deep);dfs(node.right,deep);}
}
bfs做法
class Solution {//结果集public ListListInteger res new ArrayListListInteger();public ListListInteger levelOrder(TreeNode root) {bfs(root);return res;}//bfs方式队列的方式来迭代public void bfs(TreeNode node){if(node null) return;QueueTreeNode que new LinkedList();//bfs的做法有时候会比较dfs还要固定//先入根que.offer(node);while(!que.isEmpty()){ListInteger list new ArrayList();//记录队列长度用于迭代int len que.size();while(len 0){//拿出队列中首层的节点TreeNode tmp que.poll();list.add(tmp.val);//找左右if(tmp.left!null) que.offer(tmp.left);if(tmp.right!null) que.offer(tmp.right);len--;}res.add(list);}}
} 力扣107107. 二叉树的层序遍历 II
dfs方法
class Solution {//最后进行反转即可public ListListInteger res new ArrayList();public ListListInteger levelOrderBottom(TreeNode root) {dfs(root,0);ListListInteger result new ArrayList();for (int i res.size() - 1; i 0; i-- ) {result.add(res.get(i));}return result;}public void dfs(TreeNode node,Integer deep){if(node null) return;//深度增加deep;//新的一层就要增加小结果集if(res.size() deep){ListInteger item new ArrayList();res.add(item);}//开始遍历左右//首先将该节点存入对应位置结果集res.get(deep-1).add(node.val);//找左右dfs(node.left,deep);dfs(node.right,deep);}
}
bfs做法
class Solution {//最后进行反转即可public ListListInteger res new ArrayList();public ListListInteger levelOrderBottom(TreeNode root) {bfs(root);ListListInteger result new ArrayList();for (int i res.size() - 1; i 0; i-- ) {result.add(res.get(i));}return result;}public void bfs(TreeNode node){//为空直接返回if(node null) return;QueueTreeNode que new LinkedList();que.offer(node);//然后在while里面去不断迭代while(!que.isEmpty()){//小结果集ListInteger list new ArrayList();//bfs首先都得记录自己已经入队的节点数int size que.size();for(int i 0;isize;i){TreeNode tmp que.poll();//拿出该节点后将该节点值入小结果集list.add(tmp.val);//去找左右if(tmp.left!null) que.offer(tmp.left);if(tmp.right!null) que.offer(tmp.right);}//当前层遍历完了将小结果集加入大结果集res.add(list);}}
} 力扣199199. 二叉树的右视图
bfs做法这里就不再贴dfs做法了
class Solution {//思路还是很直接用bfs做只需要判断当前遍历到的是不是最右边的就可以public ListInteger rightSideView(TreeNode root) {ListInteger res new ArrayList();DequeTreeNode que new LinkedList();if(root null) return res;que.offer(root);while(!que.isEmpty()){int size que.size();for(int i 0;isize;i){TreeNode tmp que.poll();//找左右if(tmp.left!null) que.offer(tmp.left);if(tmp.right!null) que.offer(tmp.right);//如何判断是右侧看到的只要i走到了当前层的最后一个节点if(i size - 1) res.add(tmp.val);}}return res;}
} 力扣637637. 二叉树的层平均值
bfs做法这里就不再贴dfs做法了
class Solution {public ListDouble averageOfLevels(TreeNode root) {ListDouble res new ArrayList();DequeTreeNode que new LinkedList();if(root null) return res;que.offer(root);while(!que.isEmpty()){int size que.size();double sum 0.0;for(int i 0;isize;i){TreeNode tmp que.poll();//计算总值sum tmp.val;//找左右if(tmp.left!null) que.offer(tmp.left);if(tmp.right!null) que.offer(tmp.right);}res.add(sum / size);}return res;}
} 力扣429429. N 叉树的层序遍历
bfs做法这里就不再贴dfs做法了
/*
// Definition for a Node.
class Node {public int val;public ListNode children;public Node() {}public Node(int _val) {val _val;}public Node(int _val, ListNode _children) {val _val;children _children;}
};
*/class Solution {public ListListInteger res new ArrayList();DequeNode que new LinkedList();public ListListInteger levelOrder(Node root) {//都通过bfs来做会快很多if(root null) return res;que.offer(root);while(!que.isEmpty()){//记录当前大小int size que.size();ListInteger list new LinkedList();for(int i 0;isize;i){Node tmp que.poll();list.add(tmp.val);//找孩子ListNode children tmp.children;//如果没孩子就继续即可if(children null || children.size() 0) continue;for(Node child : children){//有孩子就一个个找出来放到队列里面去if(child ! null){que.offer(child);}}}//将该层加入大结果集res.add(list);}return res;}
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/88839.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!