1 Star 0 Fork 0

表情扭曲 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc199.java 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
liu13 提交于 2019-07-06 14:01 . 20190706
package code;
/*
* 199. Binary Tree Right Side View
* 题意:二叉树的右视图
* 难度:Medium
* 分类:Tree, Depth-first Search, Breadth-first Search
* 思路:1.记录当前递归最大深度,每次把第一次出现深度的节点值输出即可
* 2.层次遍历
* Tips:
*/
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class lc199 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
int max_depth = 0;
List<Integer> res = new ArrayList();
public List<Integer> rightSideView(TreeNode root) {
dfs(root, 1);
return res;
}
public void dfs(TreeNode root, int depth){
if(root==null) return; //别忘了null
if(depth>max_depth) {
max_depth = depth;
res.add(root.val);
}
dfs(root.right, depth+1);
dfs(root.left, depth+1);
}
public List<Integer> rightSideView2(TreeNode root) {
List<Integer> res = new ArrayList();
Queue<TreeNode> qu = new LinkedList(); //是一个队列,用LinkedList
if(root!=null) qu.add(root);
while(!qu.isEmpty()){
int size = qu.size();
while(size>0){
TreeNode tn = qu.remove();
if(size==1) res.add(tn.val); //==1的时候添加
if(tn.left!=null) qu.add(tn.left);
if(tn.right!=null) qu.add(tn.right);
size--;
}
}
return res;
}
}
1
https://gitee.com/abfantasy/leetcode.git
git@gitee.com:abfantasy/leetcode.git
abfantasy
leetcode
leetcode
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891